Writing to a NFC Tag on Android

Peter-John Welcome
AndroidPub
Published in
3 min readJul 30, 2017
https://advicesacademy.com/wp-content/uploads/2015/02/NFC-on-Android.png

Recently, there has been a lot of buzz around NFC. This is due to Apple now allowing iOS developers to develop with this technology within their apps. If it’s going to be something that can be done on iOS and Android, why not get into playing around with this API again. So I thought, let me start with writing to an NFC tag.

Getting Started

We will start in our Android Manifest. In here we have a few things we need to setup. First, we need to add permission for NFC.

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

Following this, we want to make sure that users using our app, have NFC on their device. For this we add a uses-feature.

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

Lastly, we need to create an intent-filter on the activity we will be on when writing to the NFC tag. This is so that the NFC tag can be discovered by this activity. Within this intent-filter, we create an action tag and a data tag as seen below. The pathPrefix is something we will need later, so I generally make it something relating to the app.

<activity android:name=".MainActivity">           
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:host="ext" android:pathPrefix="/peterjohnwelcome.com:nfcapp" android:scheme="vnd.android.nfc" />
</intent-filter>
</activity>

Lets get into the Code!!!

NB: All code is written in Kotlin.

For us to be able to write something to the NFC tag, we will need to create some kind of message payload. So I created a method called createNFCMessage. What this method does is, it takes the pathPrefix we defined in our Manifest, creates a NFCRecord that will be used to create a NDefMessage. This is the message we will be writing to the NFC tag.

At this point, we need a writeMessageToTag method that will connect to the NFC Tag and write the payload we supply to the Tag.

We have two more methods we need to write. First, we need an enableNFCInForeground method. What this method does is create a PendingIntent that gets created on top of our activity. Why we need this is because we want our activity to be able to discover only NFC (ACTION_NDEF_DISCOVERED) IntentFilters. This is what we added to the Manifest earlier.

We will also want to disable this once we have exited our activity, so we will create a disableNFCInForeground method. The method just removes the PendingIntent from our activity.

Now we move to the activity where we get to use these methods we have created.

We Almost there =)

At this point we have all our methods we need to write a message to the NFC Tag, so lets glue it all together.

We first create a NFCAdapter. We then initialize it within our onCreate. We will then need to enable our PendingIntent to run in the foreground of our activity that we created. This we do in the onResume method. We will disable this PendingIntent on our onPause method.

Where all the magic happens, is when we override the onNewIntent method. This method is activated when the NFC tag touches our device. This is because of the IntentFilter that we set.

In the onNewIntent method, we will call the createNFCMessage method with our payload we would like to write to the NFC tag. We will then receive a true or false value, stating if we wrote to the Tag successfully.

Thats it. We have successfully written to the NFC tag.

In a follow up article, I’ll be showing how we can read the message we wrote to the NFC tag. So go try this out, and stay tuned for the next part of this NFC series.

For a more complete example of the code above, you can visit my Github page:

https://github.com/pjwelcome/NFC_Android_Tutorial

Let me know what you think in the comments.

Get in Touch!

Peter John Welcome — Google+

Thanks to Ashton Welcome and Joshua Leibstein for reviewing this post.

--

--

Peter-John Welcome
AndroidPub

Freelance Senior Mobile Engineer, Google Developer Expert for Firebase, Photographer