record NFC tagrecordrecord 1 TNFABSOLUTEURIrecord Ndef Record nfc
程式建立record的方法 NFC tag中的資料是儲存在record中,如果程式中需要建立record,可以參 考下列程式碼: 1. 建立TNF_ABSOLUTE_URI型態的record Ndef. Record nfc. Record = new Ndef. Record(Ndef. Record. TNF_ABSOLUTE_URI, "http: //developer. android. com/index. html". get. Bytes(Charset. for. Name("US-ASCII")), new byte[0]); 2. 建立TNF_MIME_MEDIA型態的record Ndef. Record nfc. Record = new Ndef. Record(Ndef. Record. TNF_MIME_MEDIA, "application/com. example. android. beam". get. Bytes(Charset. for. Name("US-ASCII")), new byte[0], "Beam me up, Android!". get. Bytes(Charset. for. Name("US-ASCII"))); 3. 建立TNF_WELL_KNOWN型態的record並儲存RTD_TEXT型態的資料 byte[] lang. Bytes = locale. get. Language(). get. Bytes(Charset. for. Name("US-ASCII")); Charset utf. Encoding = encode. In. Utf 8 ? Charset. for. Name("UTF-8") : Charset. for. Name("UTF-16"); byte[] text. Bytes = payload. get. Bytes(utf. Encoding); int utf. Bit = encode. In. Utf 8 ? 0 : (1 << 7); char status = (char) (utf. Bit + lang. Bytes. length); byte[] data = new byte[1 + lang. Bytes. length + text. Bytes. length]; data[0] = (byte) status; System. arraycopy(lang. Bytes, 0, data, 1, lang. Bytes. length); System. arraycopy(text. Bytes, 0, data, 1 + lang. Bytes. length, text. Bytes. length); Ndef. Record nfc. Record = new Ndef. Record(Ndef. Record. TNF_WELL_KNOWN, 3 Ndef. Record. RTD_TEXT, new byte[0], data);
程式建立record的方法 4. 建立TNF_WELL_KNOWN型態的record並儲存RTD_URI型 態的資料 byte[] uri. Field = "example. com". get. Bytes(Charset. for. Name("US-ASCII")); byte[] payload = new byte[uri. Field. length + 1]; //加 1是為了URI的前置字元 byte payload[0] = 0 x 01; //在URI前面加上http: //www. System. arraycopy(uri. Field, 0, payload, 1, uri. Field. length); //在payload後面加上URI Ndef. Record nfc. Record = new Ndef. Record( Ndef. Record. TNF_WELL_KNOWN, Ndef. Record. RTD_URI, new byte[0], payload); 5. 建立TNF_EXTERNAL_TYPE型態的record byte[] payload; . . . (把資料存到payload中) Ndef. Record nfc. Record = new Ndef. Record( Ndef. Record. TNF_EXTERNAL_TYPE, "example. com: external. Type", new byte[0], payload); 4
程式建立record的方法 準備好NFC record之後就可以建立一個NFC message將 record包裝起來: Ndef. Message msg = new Ndef. Message( new Ndef. Record[] { nfc. Record }); 如果要在NFC message中放入多個record,可以參考以 下程式碼: Ndef. Record nfc. Record 1 = new Ndef. Record(…); Ndef. Record nfc. Record 2 = new Ndef. Record(…); Ndef. Record nfc. Record 3 = new Ndef. Record(…); Ndef. Message msg = new Ndef. Message( new Ndef. Record[] { nfc. Record 1, nfc. Record 2, nfc. Record 3 }); 5
- Slides: 8