NFC From Scratch (With a Practical Example)

Carlos Javier Torres Pensa
Flux IT Thoughts
Published in
4 min readMay 26, 2021

--

Before we delve into an example to implement NFC (Near Field Communication) on Android, we’d like to dig a little deeper into this interesting technology.

With the evolution of technology, the human being has always sought to simplify the ways to control it; this entails abstracting all the engineering behind the devices and exposing a minimal interface that allows us to use them. By way of analogy, we can compare sailing a yacht at sea with a commercial aircraft’s autopilot. Or, to get closer to the ground, let’s think about cellphones with buttons versus the modern ones that have face recognition features. In each case, not only were there improvements enabled by technology, but the interfaces to control devices have also evolved, thus avoiding manual use through buttons or touchscreens.

In the information age, we are determined to connect with everything and everyone around us in the most natural and intuitive way. This is where NFC technology arises: it allows us to wirelessly pair and connect devices over short distances instantly.

So, What Is NFC?

Thanks to electromagnetic radio fields, we only have to place the NFC devices within, at least, 10 cm of each other for them to exchange data.

NFC has two communication modes: the passive mode, which allows us to read and receive static data from another device (for example, to get configuration data or an URL); and the active mode, which usually allows us to send or write variable data according to the need of a particular moment (for example, to make electronic payments).

These are some of the uses enabled by NFC:

  • Mobile payments
  • Contactless credit card payments
  • Identification
  • Instant synchronization or pairing of Bluetooth devices.
  • Sending or receiving contact information
  • Automatizations with NFC tags
  • Smartphone configuration (for example, to enable the airplane mode)

How Secure Is NFC?

At first, this technology could be used with a locked smartphone, but this functionality has been modified a while ago. Nowadays, in order to use NFC, we have to unlock the device (through a pin, fingerprint, or facial recognition). In addition, we can only use this technology in close proximity.

Devices allow users to turn on/off the NFC feature in the configuration menu, and it is advisable to keep it off until we need to use it.

An Application Example

We’ll learn how to implement NFC on Android with a native method.

We’ll write a URL on an NFC tag, which we’ll later use to open that website on any Android device. The case that follows can be used, for example, for restaurant menus, price lists, to find out the price of a particular item, etc.

To get started, we have to add the NFC permissions to our project.

We create an activity that will allow us to enter a URL to write that value on an NFC tag. In the activity layout, we include a textView and a button to execute changes.

Next, we verify whether the device is compatible with NFC technology through the NfcAdapter instance. If it is, we’ll check if the NFC antenna is enabled as well.

Additionally, we implement the NfcAdapter.ReaderCallback interface in our Activity, which will make the onTagDiscovered(tag: Tag) overwritten method available. Its purpose is to let us know when our device detects the NFC tag and it will try to write our encapsulated URL on a NdefMessage instance.

Finally this is our BeamActivity and the access to the entire project.

Here’s a video for you to see it all applied as well as its use in real time:

NFC is a pretty simple technology on its own. However, when we combine it with other technologies, we create a secure and powerful communication interface, which, used with smartphones or tags, for instance, gives us the opportunity to interact with hundreds of devices, being able to create “buttons” wherever we need to interact, be it on a different device or environment.

We believe that the main issue when it comes to implementing NFC on Android is the lack of documentation. This technology has a series of variables regarding reading and writing methods and times, sometimes due to the manufacturer or the system configuration. Although it isn’t something that can’t be solved with a few hours (or even days) of testing, it’s key to take it into account when it comes to implementing this technology.

We hope you find this article helpful at a technical level and it also helps you save some time; and, of course, we’d be thrilled if you also find it inspiring!

Authors:

Code: Marcos De Cristóbal

Writing: Carlos J. Torres P.

Know more about Flux IT: Website · Instagram · LinkedIn · Twitter · Dribbble · Breezy

--

--