Get data from near field communications Ndef tag records. For code examples, see Rhodes Webinar Sample: NFC and Rhodes System API Samples: NFC.
To use the NFC methods, you need to enable NFC on the Android device and ensure that the version is 2.3.3 or later. Do this by adding that capability to the build.yml file:
android: version 2.3.3 extensions: - nfc
An Ndef record consists of a hash.
ID = 'id' TNF = 'tnf' TYPE = 'type' PAYLOAD = 'payload'
The Type Name Format (TNF part of the hash) is a 3-bit field that indicates how you interprete the type field (TYPE). Here is a list of the TNF values.
TNF_ABSOLUTE_URI = 3 TNF_EMPTY = 0 TNF_EXTERNAL_TYPE = 4 TNF_MIME_MEDIA = 2 TNF_UNCHANGED = 6 TNF_UNKNOWN = 5 TNF_WELL_KNOWN = 1
The RTD text type (TYPE part of the hash) is used with the TNF_WELL_KNOWN value.
RTD_TEXT = [0x54] # "T" RTD_URI = [0x55] # "U" RTD_SMART_POSTER = [0x53, 0x70] # "Sp" RTD_ALTERNATIVE_CARRIER = [0x61, 0x63] # "ac" RTD_HANDOVER_CARRIER = [0x48, 0x63] # "Hc" RTD_HANDOVER_REQUEST = [0x48, 0x72] # "Hr" RTD_HANDOVER_SELECT = [0x48, 0x73] # "Hs"
This example creates a hash for an NdefRecord, which will be part of an NFCMessage to be pushed to an NFC-capable mobile device.
hash = { 'id' => [0], 'type' => Rho::NdefRecord::RTD_URI, 'tnf' => Rho::NdefRecord::TNF_WELL_KNOWN, 'payload' => payload}
You can extract data from an Ndef record with the following methods:
Rho::NdefRecord.get_id # returns byte[] Rho::NdefRecord.get_tnf # returns int Rho::NdefRecord.get_type # returns byte[] Rho::NdefRecord.get_payload # returns byte[] Rho::NdefRecord.get_payload_as_string # returns string Rho::NdefRecord.get_byte_array # returns byte[]
Returns a hash from an Ndef record, from which you can then extract data with the NdefRecord get methods. Click here for an example in the Rhodes developer guide under NFC.
Rho::NdefRecord.make_hash
The hash has the following format:
id |
Array of bytes. The tag ID. |
tnf |
int. The Type Name Format. |
type |
array of bytes |
payload |
Array of bytes. The tag payload. |
payload_as_string |
String. The message payload in a string (support special formats for URI, TEXT). |
Returns a string that is a text description of the Type Name Format.
Rho::NdefRecord.convert_Tnf_to_string(tnf)
tnf |
Int. The Type Name Format (TNF) from an Ndef record hash. |
Returns a string that is a text description of the rtd (type part of the NdefRecord hash).
Rho::NdefRecord.convert_RTD_to_string(rtd)
rtd |
A byte array. The rtd (type) from an NdefRecord hash. |