NFC Basics – Part 1

In this post, we would be covering basics of NFC and the random question which wobble in our mind associated with this.

Following are some of the generic questions which we will be answering as we go along in our post :

  • What is NFC?
  • What is the usage of NFC?
  • How android make use of NFC?
  • How is data encapsulated in NFC tag?
  • How Android read data from NFC tag?
  • How is NFC Tag identified by the different application installed on the device?

NFC (near field communication) is a communication protocol which allows two devices to communicate and transfer data when they are in proximity to each other. Typically minimum distance required for the connection is 4cm or less.

NFC helps in sharing the small amount of data between NFC tags and an Android-powered device. Android has the most support for NDEF standard, which is defined by NFC Forum. NFC data in android can be sent and received in the form of NDEF messages.

When working with these NDEF messages, we can understand how:

  1. Read/Write operation with NFC tags are performed
  2. Two devices can exchange data with each other using Android Beam

First, let’s understand how NFC data is handled by the system.

NFC data (NDEF data) is read in the form of NDEF messages with the help of tag dispatch system, which analyses scanned NFC tags, parses them and tries to locate applications that are interested in the scanned data.

  • An Application which wants to handle the NDEF data of the scanned NFC tag can declare an Intent Filter and request to handle the data.
  • When a tag is brought near to the NFC enabled device, the tag is parsed and the MIME type or the URI of the payload is identified.
  • Identified MIME type/URI and the payload is encapsulated in the Intent object
  • The activity corresponding to handle such type of payload is launched.

Composition of NDEF data

  • NDEF data is encapsulated inside a message(NDEF messages)
  • NDEF messages contain one or more records(NDEF records)
  • When system want to read NDEF data, first NDEF record inside the NDEF message is read to determine how to interpret the entire NDEF message

Structure of first NDEF record

  • 3-bit TNF(Type Name Format)
    • It defines how to interpret the variable length field i.e. it describes the record type and sets the expectation for the structure and the content of the rest of the record. Its value can be Empty Record, Well Known Record, MIME Media Record etc.
  • Record TYPE
    • It defines the type of the record. The value of type field must correspond to the value entered in the TNF bits.
  • Record ID
    • It is the unique identifier for the record
  • PAYLOAD
    • It is the actual data payload that you want to read or write.

Tag Dispatch System uses the TNF and type fields to map a MIME type or URI to the NDEF messages and following next actions steps are taken:

  • If the mapping is successful, Tag Dispatch System encapsulate that information inside ACTION_NDEF_DISCOVERED intent with the actual payload.
  • When the mapping is unsuccessful, due to any reason (even when NFC tag does not contain NDEF data), the information is encapsulated inside ACTION_TECH_DISCOVERED intent.
  • NDEF records are of the different type such as:
    • TNF_ABSOLUTE_URI
      • Defines that type field contains an absolute-URI
    • TNF_EMPTY
      • Defines the record is empty
    • TNF_UNKNOWN
      • Defines payload type is unknown
  • When a tag dispatch system comes across a record of type TNF_ABSOLUTE_URI, it maps the variable length type field into a URI and then encapsulates the URI in the data field of ACTION_NDEF_DISCOVERED.

So once the intent is created with the desired information encapsulated in it, what happens now?

Once everything is in place this intent is sent to the interested application that has set the filters for this intent. There might be one or more than one application that can handle such type of intent, in such a scenario the Activity chooser is presented to the user to select the activity. Tag Dispatch System has defined the priority of intent that one can set to the activity in order to get priority over the other type of intent.

  • ACTION_NDEF_DISCOVERED
  • ACTION_TECH_DISCOVERED
  • ACTION_TAG_DISCOVERED

So when a tag is brought near to the NFC enabled device, tag dispatch system tries to start an activity, with the intent created by it,  which has set ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED filter.If not found then it searches for activity which has set ACTION_TECH_DISCOVERED or ACTION_TAG_DISCOVERED. If no application is found for any of the intents, do nothing.

As a general advice, you should always use ACTION_NDEF_DISCOVERED intent, because it is the most specific out of the three. This intent starts the application at a more appropriate time than the other two intents, giving the user a better & richer experience.

In the next post, we would cover how to parse the intent object in our android application.Stay tuned!!