Create a NFC Reader Application for Android

Add NFC Permission to your Application

First, we need to add NFC Permission in the Android Manifest of our Application. We also define that the application will use the NFC feature. It is useful to hide such application from devices having no NFC support on the Google Play Store for example :

Parse data from a NFC Tag

To parse the date read from a NFC Tag, we are going to use some classes created by Google for the Android Open Source Project in demos showing how to use NFC.

  • ParsedNdefRecord interface exposing the str method returning the String data read from the NFC Card. Then, we will have 3 implementations
  • One for UriRecord implementing the str method and offering an isUri method letting you to know if data read from a NFC Card or Tag can be considered as an URI
  • Another for TextRecord based on the same principle
  • Another for SmartPoster which combines an URI and a Text

Creating a NDEF Message Parser

Data exchanged via NFC use the NDEF format. NDEF means NFC Data Exchange Format. So, to parse the data exchanged, we are going to create a NdefMessageParser class. It will expose a static parse method taking in parameter a NdefMessage and returning the content of this message as a list of ParsedNdefRecord instances.

Creating the User Interface

Now, we can create the User Interface of our NFC Reader Application. This interface will be really simple with just an ImageView representing a logo and a TextView used to display the data read from the NFC Tag or Card scanned.

Java Code of the NFC Reader App

Now, it’s time to write the Java Code of the Main Activity. Like previously, we will use in the MainActivity class some methods offered by Google for the Android Open Source Project :

  • dumpDataTag which takes a Tag object in parameter and returns the data dumped from this tag in String format
  • toHex, toReversedHex, toDec and toReversedDec utilitaries static methods letting us to print bytes in Hexadecimal an Decimal format from the data read on the Tag or the Card

Testing the NFC Reader App

Now, it’s time to try our NFC Reader Application built in this tutorial. Once the application is launched on real device, we have to scan a NFC Card or Tag to get the following screen :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sylvain Saurel

Sylvain Saurel

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://inbitcoinwetrust.substack.com/