Near Field Communication: Reading Tags With Android

By September 4, 2018 April 8th, 2019 Development, Enterprise Mobility

Near field communications (NFC) is an interesting technology that is built into most modern smartphones. Recently I read an article outlining how to read NFC tags on Android. The article did a good job at giving a basic overview of the implementation, but using the steps described left me with some questions and for my use case it did not work.

I’ve created this primer as a condensed explanation and implementation for those looking to add reading of Near Field Communication tags to their own Android project.



As with any Android application, you need to add permissions to allow your application to use the devices’ NFC hardware.

Within your Android.Manifest file, add the following:

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

If you’re releasing your application to the Google Play Store add the following to your manifest to ensure your application only shows up for those devices that support NFC.

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

To really cover your bases, you might want to ensure the device’s minimum Android SDK is API level 10 (or 16 for Android Beam support) for complete NFC functionality:

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

Intent filters

Next, you’ll want to set up an intent filter so the device launches your application upon detecting and reading a tag. The official Android documentation on NFC intent filtering is a great resource to learn more about NFC intents, including the “what”, “why”, and “how” to use them in various use-cases.

Here is an example intent filter.

   <action android:name=“android.nfc.action.NDEF_DISCOVERED” />
   <category android:name=“android.intent.category.DEFAULT” />
   <data android:mimeType=“application/your.uniqueapp.nfc”  />

What is the Mime Type on line 4? That is used by Android to determine which application should handle the NFC Tag. When creating yours, make sure it is unique to your application. In the example above “your.uniqueapp.nfc” can be “”.

The idea of having a unique Mime Type on the NFC tags is 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 applications the user will more likely than not pull the phone away from the NFC tag and sever the connection.

Application Code           nfc

Now for the fun part, the Code!

Android’s NFC Service will send your application the tag data using intents. Here is a simple method to process that intent. The code is assuming your getting NDEF message formatted data.

private void processNFCData( Intent inputIntent ) {

    Parcelable[] rawMessages =

    if (rawMessages != null && rawMessages.length > 0) {

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

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

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

        Log.i(TAG, "message size = " + messages.length);

        // only one message sent during the Android beam
        // so you can just grab the first record.
        NdefMessage msg = (NdefMessage) rawMessages[0];

        // record 0 contains the MIME type, record 1 is the AAR, if present
        String payloadStringData = new String(msg.getRecords()[0].getPayload());

        // now do something with your payload payloadStringData


Now, where should you invoke processNFCData? There are two places: onNewIntent and onResume. Why two places? Depends on your use case. If you’re reading a tag, then use onNewIntent. If you’re using Android Beam, then onResume.

protected void onNewIntent(Intent intent) {

    if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals( intent.getAction() )) {
         // We scanned an NFC Tag.
         processNFCData( intent );


protected void onResume() {

    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals( getIntent().getAction() )) {
        // Yes, Activity start via Beam...  wonder if we should pass a flag indicating Beam?
        processNFCData( getIntent() );

Next Steps

From here, you will need to add additional logic to process your tag’s data. As you’re working with NFC, make sure to add checks to verify that NFC is available and turned on. These can be accomplished by using the NfcAdapter class. Information about this class can be found within the Android Developer documentation.

If you still need some code to look at, I put together a simple application that uses Android Beam to send data. You can find the project on GitHub. The main part of the code is in the MainActivity, which can be found here.

NFCIn Closing

Near Field Communication is a relatively simple technology which can add useful functionality to many applications. The ability to quickly store and read data by tapping a device can be very useful.

Resources Referenced in this article.

Android Developers Guide on NFC

Android Developer NfcAdapter Guide

Android NFC Beam

Simple NFC Demo App

Related Posts

Brock Davis

Brock Davis

Technical Project Manager - More than a decade of experience in leading engineering teams in the design and implementation of applications, applying technical knowledge to solve complex problems, building highly scalable online web applications, and seeking client focused solutions, while mentoring and training junior engineers.