Saturday 28 September 2013

Near Field Communication


Near Field Communication (NFC) is a set of short-range wireless technologies, typically requiring a distance of 4cm or less to initiate a connection. NFC allows you to share small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices.

Tags can range in complexity. Simple tags offer just read and write semantics, sometimes with one-time-programmable areas to make the card read-only. More complex tags offer math operations, and have cryptographic hardware to authenticate access to a sector. The most sophisticated tags contain operating environments, allowing complex interactions with code executing on the tag. The data stored in the tag can also be written in a variety of formats, but many of the Android framework APIs are based around a NFC Forum standard called NDEF (NFC Data Exchange Format).

There are two major uses cases when working with NDEF data and Android:

  • Reading NDEF data from an NFC tag: Analyzes discovered NFC tags, appropriately categorizes the data, and starts an application that is interested in the categorized data.
  • Beaming NDEF messages from one device to another with Android Beam™: No manual device discovery or pairing is required,connection is automatically started when two devices come into range.

How NFC tags are mapped to MIME types and URIs:

NDEF data is encapsulated inside a message (NdefMessage) that contains one or more records (NdefRecord). Each NDEF record must be well-formed according to the specification of the type of record that you want to create. Android also supports other types of tags that do not contain NDEF data, which you can work with by using the classes in the android.nfc.tech package. To learn more about these technologies, see the Advanced NFC topic. Working with these other types of tags involves writing your own protocol stack to communicate with the tags, so we recommend using NDEF when possible for ease of development and maximum support for Android-powered devices.

How Android handles NDEF formatted tags:

When an Android-powered device scans an NFC tag containing NDEF formatted data, it parses the message and tries to figure out the data's MIME type or identifying URI. To do this, the system reads the first NdefRecord inside the NdefMessage to determine how to interpret the entire NDEF message (an NDEF message can have multiple NDEF records). In a well-formed NDEF message, the first NdefRecord contains the following fields:

3-bit TNF (Type Name Format)

  Indicates how to interpret the variable length type field.

Variable length type

 Describes the type of the record. If using TNF_WELL_KNOWN, use this field  to specify the Record Type Definition (RTD).

Variable length ID

 A unique identifier for the record. This field is not used often, but if you need  to uniquely identify a tag, you can create an ID for it.

Variable length payload

 The actual data payload that you want to read or write. An NDEF message  can contain multiple NDEF records, so don't assume the full payload is in the  first NDEF record of the NDEF message.

     The tag dispatch system uses the TNF and type fields to try to map a MIME type or URI to the NDEF message. If successful, it encapsulates that information inside of a ACTION_NDEF_DISCOVERED intent along with the actual payload. However, there are cases when the tag dispatch system cannot determine the type of data based on the first NDEF record. This happens when the NDEF data cannot be mapped to a MIME type or URI, or when the NFC tag does not contain NDEF data to begin with. In such cases, a Tag object that has information about the tag's technologies and the payload are encapsulated inside of a ACTION_TECH_DISCOVERED intent instead.

How NFC Tags are Dispatched to Applications:

    When the tag dispatch system is done creating an intent that encapsulates the NFC tag and its identifying information, it sends the intent to an interested application that filters for the intent. If more than one application can handle the intent, the Activity Chooser is presented so the user can select the Activity. The tag dispatch system defines three intents, which are listed in order of highest to lowest priority.

Note:

  • "NdefMessage" Represents an immutable NDEF Message.
  • "NdefRecord" Represents an immutable NDEF Record.
  • Whenever possible, work with NDEF messages and the ACTION_NDEF_DISCOVERED intent, because it is the most specific out of the three. This intent allows you to start your application at a more appropriate time than the other two intents, giving the user a better experience.

about the post:
  • basic post on NFC (Near Field Communication) in android.
  • we will cover NFC with code examples in coming post.