Nfc lab8月定例会

19
2次元と仲良くしよう と思いました NFCLab 8月定例会 下川 敬弘 12825日土曜日

description

 

Transcript of Nfc lab8月定例会

Page 1: Nfc lab8月定例会

2次元と仲良くしようと思いました

NFCLab 8月定例会 下川 敬弘

12年8月25日土曜日

Page 2: Nfc lab8月定例会

自己紹介

株式会社ソニックムーブ(神楽坂)

フロントエンド エンジニア

NFCLab, JAG, JAG茨城支部

@androhi

12年8月25日土曜日

Page 3: Nfc lab8月定例会

アプリ作ってみてます

12年8月25日土曜日

Page 4: Nfc lab8月定例会

NDEFメッセージ ⇒ QRコード

Library : ZXing 2.0

概要

12年8月25日土曜日

Page 5: Nfc lab8月定例会

想定する利用シーン

A「このポスターにタッチすると、スマートフォンで何か見られるらしいぞ。」

B「どれどれ…。?。何も起きないけど。」

C「あっ!おサイフケータイには、順次対応予定ですって書いてある!」

D「私のも、おサイフケータイだからダメだわ。」

あなた「まかせろっ!(」・ω・)」うー!(/・ω・)/にゃー!」

ABCD「キャー!ステキー!」

12年8月25日土曜日

Page 6: Nfc lab8月定例会

勉強になったコード

12年8月25日土曜日

Page 7: Nfc lab8月定例会

NDEFメッセージ読み込み private String getUrl(NdefMessage msg) {

// NDEFレコードからTNFを取り出す NdefRecord[] records = msg.getRecords(); short tnf = records[0].getTnf();

if (tnf == NdefRecord.TNF_WELL_KNOWN) { return parseWellKnown(records[0]); } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) { return parseAbsolute(records[0]); }

throw new IllegalArgumentException("Unknown TNF " + tnf); }

12年8月25日土曜日

Page 8: Nfc lab8月定例会

TNF = Type Name Format

TNF_ABSOLUTE_URI

URIをベースとしたタイプフィールド

TNF_WELL_KNOWN

設定したRTDに応じて、振る舞いが変わる

12年8月25日土曜日

Page 9: Nfc lab8月定例会

NDEFレコード読み込み private String parseAbsolute(NdefRecord record) { byte[] payload = record.getPayload(); return new String(payload, Charset.forName("UTF-8")); }

private String parseWellKnown(NdefRecord record) { // RTD Uriかチェック if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { byte[] payload = record.getPayload(); String prefix = prefixMap.get(payload[0]); byte[] uriData = Arrays.copyOfRange(payload, 1, payload.length); String uri = new String(uriData, Charset.forName("UTF-8")); return prefix.concat(uri); } throw new IllegalArgumentException("Not Support RTD"); }

12年8月25日土曜日

Page 10: Nfc lab8月定例会

RTD = Record Type Definition

RTD_URI

URIをベースとしたPayload

12年8月25日土曜日

Page 11: Nfc lab8月定例会

Recordsマッピング

Name Offset Size Value Description

Identifiercode 0 1 byte

URI identifier

code

The URI identifier code, as specified in

Table 3.

URIfield 1 N UTF-8 string

The rest of the URI, or the entire URI (if identifier code is

0x00).

引用:NFC URI RTD Technical Specificationhttp://www.nfc-forum.org/specs/spec_list/#rtds

12年8月25日土曜日

Page 12: Nfc lab8月定例会

Decimal Hex Protocol

0 0x00 N/A

1 0x01 http://www.

2 0x02 https://www.

3 0x03 http://

4 0x04 https://

5 0x05 tel:

6 0x06 mailto:

7 0x07 ftp://anonymous:anonymous@

8 0x08 ftp://ftp.

9 0x09 ftps://

10 0x0A sftp://

11 0x0B smb://

12 0x0C nfs://

13 0x0D ftp://

14 0x0E dav://

15 0x0F news:

16 0x10 telnet://

17 0x11 imap:

Decimal Hex Protocol

18 0x12 rtsp://

19 0x13 urn:

20 0x14 pop:

21 0x15 sip:

22 0x16 sips:

23 0x17 tftp:

24 0x18 btspp://

25 0x19 btl2cap://

26 0x1A btgoep://

27 0x1B tcpobex://

28 0x1C irdaobex://

29 0x1D file://

30 0x1E urn:epc:id:

31 0x1F urn:epc:tag:

32 0x20 urn:epc:pat:

33 0x21 urn:epc:raw:

34 0x22 urn:epc:

35 0x23 urn:nfc:

36...255 0x24...0xFF RFU

URI identifier code

12年8月25日土曜日

Page 13: Nfc lab8月定例会

private String getUrl(NdefMessage msg) {

// NDEFレコードからTNFを取り出す NdefRecord[] records = msg.getRecords(); short tnf = records[0].getTnf();

if (tnf == NdefRecord.TNF_WELL_KNOWN) { // URLを取り出す

return parseWellKnown(records[0]); } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {

// URLを取り出す return parseAbsolute(records[0]); }

throw new IllegalArgumentException("Unknown TNF " + tnf); }

12年8月25日土曜日

Page 14: Nfc lab8月定例会

QRコード生成は、以下を参考にしました。

ブログ:明日の鍵

ZXingを使ってQRコードを表示する

http://d.hatena.ne.jp/tomorrowkey/20091114/1258206013

12年8月25日土曜日

Page 15: Nfc lab8月定例会

public Bitmap createQRCodeByZxing(String contents, int size) throws WriterException { QRCodeWriter qrWriter = new QRCodeWriter(); // 異なる型の値を入れるためgenericは使えない @SuppressWarnings("rawtypes") Hashtable encodeHint = new Hashtable(); encodeHint.put(EncodeHintType.CHARACTER_SET, "shiftjis"); // エラー修復レベルを指定 encodeHint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); BitMatrix qrCodeData = qrWriter.encode(contents, BarcodeFormat.QR_CODE, size, size, encodeHint); // QRコードのbitmap画像を生成 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.eraseColor(Color.argb(255, 255, 255, 255)); for(int x = 0; x < qrCodeData.getWidth(); x++) { for(int y = 0; y < qrCodeData.getHeight(); y++) { if(qrCodeData.get(x, y)) { // trueはBlack bitmap.setPixel(x, y, Color.BLACK); } else { // falseはWhite bitmap.setPixel(x, y, Color.WHITE); } } }

return bitmap; }

12年8月25日土曜日

Page 16: Nfc lab8月定例会

QRコード生成のイメージT F T T F T T F F

F F F T T T F T F

T T F T F T T F T

T F T T F T F F F

F F T F F F T T T

F T T F F F T T F

F F T T F T F F F

T T F F T T F T F

F T T T F F T F T

12年8月25日土曜日

Page 17: Nfc lab8月定例会

まとめ1NDEFメッセージ

仕様を理解する

TNF = Type Name Format

RTD = Record Type Definition

12年8月25日土曜日

Page 18: Nfc lab8月定例会

まとめ2QRコード

ZXing超便利(遅っ)

12年8月25日土曜日

Page 19: Nfc lab8月定例会

ご清聴ありがとうございました

12年8月25日土曜日