Developer Tips: NFC Tags in App Development
Developer Tips: NFC Tags in App Development
Learn how your app can use NFC, or Near Field Communication, for geolocation, authentication, contactless payments, and more.
Join the DZone community and get the full member experience.Join For Free
Do you wonder what 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 to all your NFC questions!
What Is NFC?
Near Field Communication (NFC) is a short-range, low-power communications protocol between two devices. It allows the transfer of 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 technology is mainly aimed at 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 in a shop.
- Geolocation: Marking a 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 turnstiles check the code of an NFC pass-tag and decide whether or not to grant access.
- Internet of Things: NFC can enable a wide range of IoT devices and applications in a 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, allow the use of 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, an 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 via a public transport app by enclosing your phone to the tag.
How to Code Your First NFC Android App
How can you develop NFC in a mobile application? Here’s a short NFC programming guide.
Adding NFC Support
To enable using NFC in your app, you should add a few items to your AndroidManifest.xml file.
First, 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 the Android system detects an NFC tag and its identifying information, it sends it 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 getDefaultAdapter method. Now add the following code to your activity onCreate method:
And the 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 case, 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 (
enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in
onResume() guarantees this).
Detecting an NFC Tag and Getting 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 the 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
NFC tag data is passed in the intent parcelable extra with NfcAdapter.EXTRA_TAG. I only needed tag ID, so the
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 Tags
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.
Published at DZone with permission of Veranika Papkouskaya . See the original article here.
Opinions expressed by DZone contributors are their own.