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


Mobile technologies are constantly changing, providing users with faster and better ways to interact with the world. Devices and applications have become increasingly powerful and new technologies are continually emerging—near field communication (NFC) is one of them. Its low battery consumption and simplicity are the key factors determining its future success. From facilitating daily tasks, such as opening a website, gaining access control, and making instant payments, NFC is now being used everywhere. Features such as peer-to-peer communication, cross-device data exchange, social network content sharing, and improved gaming experiences can be achieved using NFC.

The book begins by introducing the virtual development environment that allows you to develop NFC-enabled applications without the need of a smartphone or tags. Next, this book presents simple and functional examples that explain the basic concepts of detecting, writing, and reading data from a tag. All of the presented features and techniques are then used in a more complex application to give you a better idea of how you can use NFC in a real application.

In this book, you will learn how to use different components and features, such as working with the Foreground Dispatch System, using the NFC data exchange format and the Android application record, reading and writing a URI, creating text and customizing mime NDEF messages, and so on. This book provides readers with both theoretical and practical knowledge as we progress through the recipes and explain the relevant concepts.

Toward the end of this book, the recipes will show you various ways to use NFC so that you will be pumped to start developing amazing NFC applications.

What this book covers

Chapter 1, Getting Started with NFC, covers the basic steps to create an NFC-enabled application by defining the minimum requirements and the first lines of code needed to use NFC. This chapter also covers how to set up a virtual test environment using Open NFC for an Android add-on.

Chapter 2, Detecting a Tag, introduces readers to the detection, interception, and filtering of NFC intents, using the Foreground Dispatch System and intent filters. A technical overview of the different types of tags and a series of practical examples explains how you could go about doing this.

Chapter 3, Writing Tag Content, covers the introduction to the NDEF records by explaining how this format is useful and the different possible usages. This chapter provides practical working examples that demonstrate the correct NDEF usage.

Chapter 4, Writing Tag Content – Real-life Examples, provides more detailed examples on writing different content to NFC tags by providing working application prototypes.

Chapter 5, Reading Tag Content, introduces the reading of an NFC tag content and how to handle different types of content.

Chapter 6, Reading Tag Content – Real-life Examples, covers more detailed examples on reading tag content. At the end of every recipe, a working application is created to provide insight into the NFC capabilities.

Chapter 7, Sharing Content across Devices, covers a more advanced NFC feature, peer-to-peer communication. This chapter provides working application prototypes that demonstrate how the NFC peer-to-peer feature can be used in gaming and social networking applications.

Chapter 8, Error Handling and Content Validation, covers the basics on how to prevent the NFC application from crashing by correctly handling and preventing exceptions.

Chapter 9, Extending NFC, covers how NFC can be used to complement other Android features by providing practical working examples of applications that facilitate a user's interaction with the world.

Chapter 10, Real-life Examples – NFC Utilities, covers some tips on how to make the best use of an NFC tag. This chapter also provides a working application prototype to allow for a full-learning experience.

What you need for this book

Readers must have basic knowledge of Java and Android application development.

An NFC-enabled device and physical tags are recommended but not required. This is because readers can use the virtual environment to administer development tests.

The software used during the writing of this book was Eclipse, so we recommend a working installation that is configured with Android SDK.

Who this book is for

Near Field Communication with Android Cookbook focuses on employing NFC features in useful, real-life applications. It is ideal for developers who want to learn how to create NFC-enabled Android applications. Basic Android development knowledge is preferred, but the main objective is to get readers excited about this technology! We do this by providing you with a solid grounding in the use of NFC with Android as well as some advanced examples.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "nfcbook.ch1.example1."

A block of code is set as follows:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

if (nfcAdapter != null && nfcAdapter.isEnabled()) {
  Toast.makeText(this, "NFC is available.", Toast.LENGTH_LONG).show();
} else {
  Toast.makeText(this, "NFC is not available on this device. This application may not work correctly.", 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

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

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "A Beam sent! toast should appear in the touched device and a Facebook friend request dialog should appear in the other."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.