Book Image

Near Field Communication with Android Cookbook

By : Vitor Subtil
Book Image

Near Field Communication with Android Cookbook

By: Vitor Subtil

Overview of this book

Table of Contents (17 chapters)
Near Field Communication with Android Cookbook
About the Author
About the Reviewers


We live in a world that is constantly changing, and every day, new and innovative technologies emerge which make daily life easier and more convenient. Near Field Communication (NFC) is one of these new technologies. It allows users to exchange digital content between devices with a simple touch. It is a short-range RFID technology that is currently being introduced in smartphones and used for mobile payments, mobile ticketing, marketing, identification mechanisms, and other such cool applications.

NFC is specified by the NFC Forum,, which promotes the implementation and definition of the NFC standards for interoperability of devices and services. The biggest differences among the common RFID technologies are the communication distance, which is limited to about 10 centimeters, and the ability for bidirectional communication.

By now, you are probably thinking, why do I need another adapter on my smartphone? This will be another battery-draining feature that most probably will be disabled most of the time. Well, that's not exactly the case when we talk about NFC. It is specially designed to have a very small battery consumption footprint so that it can be enabled all the time, thus avoiding the annoying rituals of enabling and disabling the adapter when we need to use it.

However, you might think you can't send large amounts of data; that's true, but it doesn't matter! We already have other good and reliable alternatives such as Bluetooth or Wi-Fi Direct for that. So, why would we need another super-fast, battery-drainer adapter? With NFC, things are kept simple. There's no need to pair devices, it can be constantly turned on, and it takes less than one-tenth of a second to establish a connection between devices.

Then you may think that what you really want is to keep the battery consumption low and still be able to transfer bigger files. In cases like these, we can use the best of both worlds. For example, we can use NFC to transfer a small data block that contains the necessary information for a successful Bluetooth pair, then turn Bluetooth on, pair the devices, transfer the files, and turn it off again. All of this is done automatically and with a seamless user experience. That too with just one tap!

NFC communication can be established between two NFC-enabled devices or between a device and a tag. One of the entities in the communication process will act as active and will be responsible for sending, reading, or writing the data, and the other will act as passive. There are three main operation modes:

  • Reading and writing: In this mode, a device reads and writes data to/from a tag.

  • Peer to Peer (P2P): In this mode, a P2P connection is established between two devices.

  • Host Card Emulation (HCE): In this mode, a device emulates the behavior of a tag/card. For example, we can use our smartphone as if we are using several payment cards.

This book will focus only on the first two modes. HCE is available from Android 4.4 KitKat, onwards and you should have a look at this amazing feature at

In this first chapter, we will create a very simple application that will allow us to cover the first steps needed in all applications that use NFC, as well as cover how to set up a working virtual testing environment.