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!
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 is mainly aimed for mobile devices. It allows simple and safe two-way interactions between electronic devices.
Main NFC modes are:
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):
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.