I was recently tasked with creating an Android application to read data from Near Field Communication tags. In the process of understanding this technology’s history, standards, and then actually writing the app, I spent a large chunk of time digging through old documentation, out of date web tutorials, and unanswered Stack Overflow questions. In the spirit of Larry Wall’s Three Virtues, I’ve created this primer as a condensed explanation and implementation for those looking to add Near Field Communication tag reading to their own Android project.

BlueFletch NFC

 

“When all else fails, read the instructions.”

An Introduction

Like Bluetooth, Wifi, and other forms of wireless signals, Near Field Communication (NFC) is a communication technology based around the idea of transmitting data over radio waves. NFC uses the same type of technology found in other products, such as mobile payment systems, RFID cards, and electronic key fobs. The main advantage of NFC over these other forms of wireless communications is its ability to store and transmit small amounts of information between a passive, unpowered storage system and an external, powered reader. And it can transmit data without having to pair devices traditionally, like with Bluetooth or Wifi. An NFC storage device can be something as small as a sticker, coin, business card, or bracelet. Additionally, most Android phones come with a built-in NFC reader. So let’s get to the good stuff.

Required Tech

Software:

Android API level 10+ (Gingerbread) is required for NFC support.
Android API level 19+ (Kit Kat) is recommended.
(optional) Android Studio

Hardware:

An Android device with NFC support
(optional) NFC tags

Implementation

To start, we’ll want our app to have permission to use the device’s NFC hardware.

Working in our Android.Manifest file, add:

<uses-permission android:name=“android.permission.NFC” />

We’ll also want to ensure the device’s minimum Android SDK is API level 10 for complete NFC functionality:

<uses-sdk android:minSdkVersion=“10” />

As a bonus, we can make sure the app shows up in the Google Play Store only for devices that have NFC:

<uses-feature android:name=“android.hardware.nfc” android:required=“true” />

Next, we’ll want to set up an intent filter so the device launches the app upon detecting and reading a tag. The official Android documentation for NFC intent filtering is a great resource to learn more about intents, including the “what”, “why”, and “how” to use them in various use-cases. In my particular case, I was only interested in reading NFC tags with a properly formatted NDEF_Message and a pre-determined, unique MimeType of application/my.uniqueapp.nfc:

 

<intent-filter>

<action android:name=“android.nfc.action.NDEF_DISCOVERED” />

<category android:name=“android.intent.category.DEFAULT” />

<data android:mimeType=“application/my.uniqueapp.nfc”  />

</intent-filter>

 

The idea of having a unique MimeType on the NFC tags in my use case was to prevent the device from having to choose between launching multiple applications. If an Android device has to prompt a user to choose between multiple NFC reading apps, for example, the user will more likely than not pull the phone away from the NFC tag and sever the connection.

So now that we’ve made sure the device has NFC capability and is able to launch the application, let’s add some logic to our Activity.java to ingest the data from the tag:

 

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

Parcelable[] rawMessages =

intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

if (rawMessages != null) {

NdefMessage[] messages = new NdefMessage[rawMessages.length];

for (int i = 0; i < rawMessages.length; i++) {

messages[i] = (NdefMessage) rawMessages[i];

}

// Process any data from the messages array.

// In my case, I created an alertDialog

}

}

}

 

Going Forward

From here, we’ll need to add additional logic to process the tag’s data. It would be wise to add error handling and logging, checks in case NFC is disabled, and what to do if the device incorrectly reads a tag.

In most use-cases, an NFC tag’s data falls into one of two categories: 1. Static information, or 2. A trigger mechanism for external information. For category 1, we could effectively transmit something like an image, audio file, or business contact. For category 2, we could make the application jump to a website, or initiate a download, or unlock a door.

Near Field Communication is a relatively simple technology which can add useful functionality to many applications. The ability to quickly store and read data at a low resource cost keeps this tech standing against the test of time.

Related Posts

Nathaniel Compton

Nathaniel Compton

Nathaniel is a recent Georgia transplant with a background in LAMP, database architecture, backend web development, machine learning, and data analysis. In his spare time he works as an advocate for code.org and stemtosteam.org.