Reading NFC Tags with Android (Kotlin)

Abhishek Bagdare
3 min readMay 7, 2021

--

Near Field Communication(NFC) Tags are used to store Data such as URLs, Contact information or even simple text. Mobile devices that support NFC Technology have the capability to read these tags.

In this post we would be talking about NFC Tags that DO NOT contain NDEF data, but instead use their custom read-write methods, such as nfcA, nfcB and other tag technologies.

(Image from Google)

PREREQUISITES :

  1. Adding the requisite permissions in the android app:
<uses-permission android:name="android.permission.NFC" />

2. Creating a Tech List XML:

Since we are dealing with non-NDEF NFC Tags, we need to specify the NFC Technologies that our app supports. This would basically be used in our app to handle intents from only the relevant Tag technologies that we wish to use.

Create a file named techlist.xml and place it under Resources →XML folder(create a new folder if it doesn’t exist).

Place a meta-data tag in your activity tag , in your manifest

3. Intent Filters :

Intent Filters are used to specify the type of intent the activity answers to. In case of our NFC Reader App, the Intent filter will be used to invoke the respective activity when the device reads an NFC tag. The invoked activity can then handle the intent.

We would be using “ACTION_TECH_DISCOVERED” for our intent filter, which is basically defined to be used to start an activity when an NFC tag is discovered that uses the technologies specified in the tech filter

4. android:launchMode

Launch Mode defines an instruction on how the activity should be launched (Really?its in the name!). We are talking about activity launch modes here as we do not want a new instance of the activity to open up every time an NFC Tag is read. For this, we have to set launch mode as “singleTop” in the manifest file. This would invoke an override method called onNewIntent( ) in our invoked activity, where we would handle the intent.

All said and done, activity tag in your android manifest file should look something like this :

You’re all set to handle the intents in your activity now!

Step 1. Create an instance of NfcAdapter and initialise it in the onCreate( ) method of your NfcReaderAdctivity

Step 2. Since we are using our lunch Mode as SingleTop , we can handle the intent in the onNewIntent( ) Method. Receive the tag object from the intent using the Tag Technology that is relevant to the tag, here we are using NFC-A tag technology.

Step 3. After receiving the NFC tag object, we can get its properties like ATQA and SAK (look them up if necessary) and then connect with the tag. We can use the isConnected( ) to see if the tag is connected.

Step 4. If the tag is connected, we can use the Transceive( ) Method to send in a byte array command that would return the requisite data stored in the card in the form of a byte array.

Here NFC_READ_COMMAND would be the custom command byte array that you would have to send to your NFC Tag in order to read it (You would probably receive this command from the manufacturer)

Step 5. Enabling Foreground Dispatch. This is done so that if the activity is already started it will take precedence over any other activity or app with the same intent filters — Basically to not handle the NFC Reader intent in our activity.

And..voila! we have read the data from the NFC Card.

--

--