Do you wonder what the NFC is? How NFC technology can be used in mobile apps? Or how to code your first Android NFC app? Well, you don’t have to anymore. Here you’ll find the answers for all your NFC questions!
WHAT THE NFC IS?
Near Field Communication (NFC) is a short-range, low-power communications protocol between two devices. It allows transferring small amounts of data wirelessly over a relatively short distance (the distance must be less than 10 cm) at rates ranging from 106 kbit/s to 424 kbit/s.
In the passive mode, the active device – the initiator (which is the device that starts the communication) – generates an electromagnetic field that powers the passive device – the target (which is the device that receives the initial signal from the initiator). Then, the target modulates this energy in order to send data back to the initiator.
In the active mode, the initiator and the target both have power supplies. The two devices use alternate signal transmissions to send data to each other. In other words, both devices generate an electromagnetic field and send data by modulating that field.
NFC MODES
NFC is mainly aimed for mobile devices. It allows simple and safe two-way interactions between electronic devices.
Main NFC modes are:
- Reader / Writer Mode
In this mode, an NFC device can read an NFC tag and write data on it. - Peer-to-Peer Mode
Two NFC devices can exchange data. - Card Emulation Mode
An NFC device appears to an external reader much the same as a traditional contactless smart card.
MAIN AREAS OF NFC USE
- Notes
Tags can be used everywhere where you need to mark an information about an object. For example, as an advertisement on a poster, a history of a museum exhibit or a product article in a shop.
- Geolocation
Marking location in space is another use of NFC tags. By using special apps, you can trigger actions when the app detects such a tag.
- Authentication
Locks, doors, and tourniquets check the code of NFC pass-tag and decide on granting or not the access.
- Internet of Things
NFC can enable a wide range of IoT devices and applications in smart home system. Thanks to their unique features, NFC specifications are the base of an excellent User Experience and extend a smart home ecosystem even to unconnected and unpowered devices at a very low cost.
- Contactless Payments
Many international payment systems like MasterCard allows using a smartphone with NFC as a credit card.
- Ticketing
Cities all over the world use NFC mobile ticketing in their public transport. In this case, NFC phone can perform the role of a transport card or read and inform the user about card balance, travel history, and other data. Also, NFC tags can be placed in buses and trams, so you can buy a ticket you need via public transport app by enclosing your phone to tag.
HOW TO CODE YOUR FIRST NFC ANDROID APP
How to develop NFC in a mobile application? Here’s a short NFC programming guide.
ADD NFC SUPPORT
To enable using NFC in your app, you should add a few items to your AndroidManifest.xml file.
Firstly, you need to add the following permission:
If you want to show your application on Google Play only for devices with NFC, add:
And the minSdkVersion for NFC is:
When Android system detects NFC tag and its identifying information, it sends the intent to an application that filters for the intent. If more than one application can handle that intent, the Activity Chooser is presented, so that the user can select the Activity. There are three types of intents in a tag dispatch system (listed in order of highest to lowest priority):
1. ACTION_NDEF_DISCOVERED
2. ACTION_TECH_DISCOVERED
3. ACTION_TAG_DISCOVERED
The image below shows how the system works after the tag has been discovered:
If you want to start your app after detection of the NFC tag, add intent-filter:
CHECKING NFC STATE
NfcAdapter class represents a local NFC adapter. To get this adapter for your Android device, call getDefaultAdaptermethod. Now add the following code to your activity onCreate method:
And method which shows an alert dialog to your activity class:
Now our app is ready to run! If you enclose an NFC tag to your device and no other app is registered for NFC intent, our app will start immediately. If other apps are installed, the activity chooser will be displayed. If you’ll run your app with the NFC turned off, it will show you a dialog offering to turn on the NFC.
NFC FOREGROUND DISPATCH SYSTEM
In this specific cas, I wanted my application to proceed an NFC tag only if it’s in the foreground. So I used the Foreground Dispatch System. This system allows an activity to intercept an intent and claim priority over other activities that handle the same intent. To enable the foreground dispatch system, add following code to activity onCreate method:
Then enable and disable the foreground dispatch when the activity loses (onPause()) and regains (onResume()) focus. enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in onResume() guarantees this).
DETECT NFC TAG AND GET ITS DATA
To process the data from the scanned NFC tag you need to implement the onNewIntent callback (it’s called for the activity after NFC tag is detected). But it’s called for activities with “singleTop” or “singleInstance” launch mode. So change your activity launch mode in AndroidManifest.xml file to:
And now, to get tag data from intent, override onNewIntent callback.
NFC tag data is passed in the intent parcelable extra with NfcAdapter.EXTRA_TAG. I only needed tag ID, so getId()method of Tag class was perfect for me, but it returns byte array, so I add a method which converts bytes array to string:
SOUNDS WHILE DETECTING TAG
Currently, only the default Android NFC tag detection sound is enabled and you can’t change or even turn off this sound programmatically. There is an issue on the Android Issue Tracker but unfortunately it’s still not closed.
* * *
As you can see, NFC technology opens new possibilities in mobile development, it can add extra functionality to your app and it’s not hard to implement with Android tools.
Sources:
http://www.dummies.com/consumer-electronics/nfc-communication-modes/
https://habrahabr.ru/article/272217/
http://nfc-forum.org
http://nfc-forum.org/wp-content/uploads/2016/06/NFC_Forum_IoT_White_Paper_-v05.pdf
https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html