最終更新:
moonlight_aska 2012年09月18日(火) 23:28:47履歴
NFCのデータ交換フォーマット(NDEF : NFC Data Exchange Format)は, NFC準拠のデバイスとNFCカード/タグとの間で通信を行う際の共通データフォーマットとして定義されている.
その中の1つに, RTD(Record Type Definition) URIフォーマットというのがある.
RTD URIフォーマットのデータは, createUriRecordメソッドで作成している.

↓(NFC TagInfoで確認)

その中の1つに, RTD(Record Type Definition) URIフォーマットというのがある.
- Payloadの構造
名前 | オフセット | サイズ | 説明 |
Identifier code | 0 | 1 byte | URI識別子コード |
URI field | 1 | N | URIの残り(UTF-8) |
- URI識別子コード
Decimal | Hex | プロトコル |
0 | 0x00 | N/A |
1 | 0x01 | http://www. |
2 | 0x02 | https://www. |
3 | 0x03 | http:// |
4 | 0x04 | https:// |
5 | 0x05 | tel: |
6 | 0x06 | mailto: |
: | : | : |
- Nfc04.java
RTD URIフォーマットのデータは, createUriRecordメソッドで作成している.
private void writeTag(Intent intent, String text) throws IOException, FormatException {
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
// Ndefメッセージ生成
NdefRecord record = createUriRecord(text);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
// Ndefメッセージの書き込み
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(msg);
Log.v("NFC", "write " + text);
ndef.close();
}
}
// RTD URIフォーマットのNdefレコード作成
private NdefRecord createUriRecord(String fullUri) throws UnsupportedEncodingException {
byte[] prefix = new byte[1];
byte[] url = null;
// URI識別子のサーチ
boolean found = false;
for (int i=1; i<uriPrefixs.length; i++) {
if (fullUri.startsWith(uriPrefixs[i])) {
prefix[0] = (byte)i;
// URIの残り
url = fullUri.substring(uriPrefixs[i].length(), fullUri.length()).getBytes("UTF-8");
found = true;
break;
}
}
if (!found) {
prefix[0] = 0x00;
url = fullUri.getBytes("UTF-8");
}
// Payloadのデータ組み立て
byte[] payload = new byte[prefix.length + url.length];
System.arraycopy(prefix, 0, payload, 0, prefix.length);
System.arraycopy(url, 0, payload, prefix.length, url.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[]{}, payload);
}
// URI識別子のテーブル
private static final String uriPrefixs[] = {
"",
"http://www.",
"https://www.",
"http://",
"https://",
"tel:",
"mailto:",
"ftp://anonymous:anonymous@",
"ftp://ftp.",
"ftps://",
"sftp://",
"smb://",
"nfs://",
"ftp://",
"dav://",
"news:",
"telnet://",
"imap:",
"rtsp://",
"urn:",
"pop:",
"sip:",
"sips:",
"tftp:",
"btspp://",
"btl2cap://",
"btgoep://",
"tcpobex://",
"irdaobex://",
"file://",
"urn:epc:id:",
"urn:epc:tag:",
"urn:epc:pat:",
"urn:epc:raw:",
"urn:epc:",
"urn:nfc:"};
- 動作例

↓(NFC TagInfoで確認)

コメントをかく