Hello ReadersJ!! Hope you enjoyed the last tutorial. Today we are going to discuss NFC which stands for Near Field Communication (NFC). This is one of the most vibrant technologies in the world of smart phones. Let us study this concept so that we can have a better idea about this section.
NFC is a set of standards which is used to establish radio communications with each other by touching the devices or bringing them in a close proximity. According to various sources these applications require contactless transactions, exchange data, and simplified set up. Communication between a NFC device and an unpowered NFC device is also possible. Only condition need to be satisfied is that there has to be an unpowered NFC chip which is also called “tag”. So NFC is a set of short-range wireless technologies. It requires an approximate distance of 4cm or less to start a connection. We can share small payloads of data between an NFC tag and an android device or between two android devices respectively.
There is a range of complexity of tags. Simple tags offers read and write semantics whereas complex tags can offer math operations. Complex tags are enabled with cryptographic hardware to authenticate access. Sophisticated tags may even contain operating environments. The data stored in tag is usually written according to NFC Forum standard called N.D.E.F (NFC Data Exchange Format). NFC enabled Android powered devices usually support three main modes of operations and they are:
- Reader/Writer mode: This mode allows NFC device to read and/or write passive tags
- P2P mode: NFC peer exchange data. This mode is used by Android beam,
- Card emulation mode: In this mode device acts as NFC card. Emulated NFC device is detectable by external NFC reader.
9.2.2 NFC Basics and Android
When we deal with NFC data we have two major use cases i.e. to read NDEF data from an NFC tag and beaming NDEF messages from one device to another via Android beam respectively. Reading NDEF data from an NFC tag is handled with the tag dispatch system. It analyzes NFC tags, categorizes data appropriately and an application is started which can make use of categorized data. An intent filter can be declared to handle the scanned NFC tag and it can also request to handle the data.
By physically tapping two devices, the android beam feature allows a device to push an NDEF message onto other device. This feature makes it more attractive and simple to use with respect to other technologies like Bluetooth, Wi-Fi, etc. With NFC we don’t require manual discovery or pairing. Whenever two devices come into each other’s range the connection is set up. Android beam is available through a set of NFC APIs. Android Beam was introduced in Android 4.0 (API level 14) which provides a simple API for an application to transmit data between two devices using NFC, simply by placing them back-to-back When an Android-powered device discovers an NFC tag, the desired behavior is to have the most appropriate activity handle the intent without asking the user what application to use. If we manually select an activity it may force the device to move away and break the connection. This is because NFC uses very short range devices. To beam messages, our application need to be in the foreground and the device receiving the data must not be locked. Android Beam is initiated by tapping two NFC-enabled Android devices together. Users are presented with a “touch to beam” UI, at which point they can choose to “beam” the foreground application to the other device.
For an application to receive NFC data, we need to add an Activity Intent Filter that listens for one of the following Intent actions:
- NfcAdapter.ACTION_NDEF_DISCOVERED: The highest priority, and most specific, of the NFC messages. Intents using this action include MIME types and/or URI data. It’s best practice to listen for this broadcast whenever possible because the extra data allows us to be more specific in defining which tags to respond to
- NfcAdapter.ACTION_TECH_DISCOVERED: This action is broadcast when the NFC technology is known but the tag contains no data or contains data that can’t be mapped to a MIME type or URI.
- NfcAdapter.ACTION_TAG_DISCOVERED: If a tag is received from an unknown technology, it will be broadcast using this action.
The tag dispatch system will determine which application should receive a particular tag based on the standard process of Intent resolution. Foreground Activity has no priority over other applications in that process. If several applications are registered to receive a tag of the type scanned, the user will be prompted to select which to use no matter even if application is at foreground. Using the foreground dispatch system, we can specify a particular Activity as having priority, allowing it to become the default receiver when it is in the foreground. Foreground dispatching can be used only while an Activity is in the foreground, so it should be enabled and disabled from within your onResume() and onPause() handlers, respectively.
By enabling Android Beam within our application, we can define the payload of the beamed message. If we don’t customize the message, the default action for our application will be to launch it on the target device. we need to request the NFC permission in the manifest:
The process to define our own custom payload is described as follows:
1. We have to create an NdefMessage object that contains an NdefRecord that will contain our message payload.
2. Then we assign our NdefMessage to the NFC Adapter as our Android Beam payload.
3. At last we configure our application to listen for incoming Android Beam messages.
To create a new NdefMessage, we have to create a new NdefMessage object that contains at least one NdefRecord containing the payload we want to beam to our application on the target device. We must specify the type of record it represents, a MIME type, an ID, and a payload. We specify our Android Beam payload using the NFC adapter. We can access the default NFC adapter using the static getDefaultAdapter method on the NfcAdapterclass. Android Beam messages are received much like NFC tags. The Activity will be launched on the recipient device when an Android Beam has been initiated, or, if our application isn’t installed, the Google Play Store will be launched to allow the user to download it.
Congratulations buddies!!! We are not going very deep in this section as it will become complex to understand. This is a big topic of discussion which will go out of our focus. See you in the next section with something new. Till then keep practicing. Happy App Developing!!