What is NFC Technology?

Before digging into the details about how to use NFC in Android, it is useful to describe a bit about NFC technology.

Near Field Technology (NFC) is a technology that enables short-range communication between two compatible devices that support this technology. NFC requires that one device behaves as the transmitter and the other one as a receiver. NFC enabled devices can be grouped in two categories:

Active

Passive

Active NFC devices are capable of sending and receiving data and can exchange data with a passive device. Passive devices can send data to other NFC-enabled devices without a power source. A typical passive device is NFC tag that can be used as advertising system for example.

NFC technology is available on the newest Android smartphones and NFC tags are used to active advertising, smart payment etc. It is important than to know how to write NFC tags in Android.

Getting Started Using NFC

The first thing an NFC-enabled Android app should do is verifying if the NFC is present and if it is active:

Just to recall if you have not read the getting started with NFC tutorial, it is necessary to register the android app so that it receives notification when the Android device is near the NFC tag. To enable this notification we have to use NFC foreground dispatch:

How To Write A NFC Tag In Android

Now the Android app is ready to handle the NFC tag and when the Android smartphone gets near the NFC tag, the event is notified to the app. The next step is writing the data on the tag. The method is quite simple:

This method accepts an abstract representation of the NFC tag we want to write and the NdefMessage containing the message to write. As the first step, the NFCManager class tries to get the Ndef tag (line 4). If the tag is null, the app tries to "format" the tag and the write the message. If the tag is already formatted, the Android app tries to connect to the tag abstract representation and write the NdefMessage.

Write URL Into NFC Tag With Android App

Now it is known how to write data into NFC tag, it is time to start writing some simple information.

The code is very simple, using NdefRecord provided by Android NFC API, the Android app creates a Uri record. As we already know, a NdefMessage is an array of record, so we create an NFC Ndef Message holding only one record: the Uri record.In this case, type holds HTTP value because it is a link.

If we want to write an NFC tag that holds a phone number so that when the user taps with the smartphone the tag a phone call is triggered, we have to pass as type tel:.

Write Text Data Into NFC Tag

The last example is writing text data in an NFC tag. In this case following NFC specs the code is very simple:

Implementing The Android UI For NFC App

The last step is implementing the UI so that the Android app handles different NFC record type and the user can insert the data to write. The app uses a spinner that holds the different record types and an EditText that holds the data to write and finally a button (a Floating Action Button) to start the operation. As soon as the user clicks on the button the app starts waiting for the NFC tag. When the user taps on the tag, the app starts writing the data.