Chapter 1. Introduction

This book started life innocently enough in an email from Brian to Tom in March 2011. Brian thought it would be a good idea to add a couple of lines to the second edition of another book about near field communication, Making Things Talk, which we were working on at the time. There was already a chapter on radio frequency identification (RFID) in the book, so how hard could it be? Two and a half years later, we’ve learned a lot about NFC along the way and picked up an excellent and knowledgeable collaborator, Don Coleman, author of the NFC plug-in for PhoneGap.

Even though NFC has a lot of potential, most of the material written about it so far hasn’t been written for the casual programmer. Everything out there assumed that if you wanted to know about NFC, you were prepared to do it from the silicon up. You had to understand the details of the various RFID specs involved, and you had to be prepared to write code that interpreted the byte stream from an NFC reader one byte at a time. While it’s useful to understand that, we figured NFC would see wider use if programmers could concentrate on what they were using it for, rather than the low-level details. Don’s PhoneGap library was the best tool we found to do just that. It lets you design NFC exchanges in the way we imagine the NFC forum designers intended: you think about the messages being exchanged and don’t worry about the rest.

Most of this book is written in that spirit. You’ll learn about the basics of the NFC Data Exchange Format (NDEF) by reading and writing messages from device to tag and from device to device. You’ll see a few sample applications—some written for PhoneGap, some for Arduino, and some for Node.js—running on embedded devices like the Raspberry Pi and the BeagleBone Black. You’ll learn some of the use patterns of NDEF, and you’ll get a taste of how you might think about the physical interaction of NFC-driven applications.

The state of the art varies from platform to platform, however. Not everything that the NFC Forum specifications describe is accessible to the casual programmer on every platform yet. We’ve attempted to give you a roadmap in this book, particularly in the later chapters, as to what the current state of development is, and where there is still room for usability improvement.

We hope that this book will help the casual programmer get a sense of what can be done using NFC, and that it will inspire more professional developers to create simple-to-use tools to help spread its use.

Who This Book Is For

You don’t have to be a trained professional programmer to read this book. We tried to write it for programming enthusiasts—people who’ve picked up some knowledge along the way, but maybe not in a formal learning setting. You won’t learn to write enterprise-level code here, but you will get a practical introduction to what near field communication is and how to program applications using it on Android, Arduino, and embedded Linux.

We assume you have some familiarity with programming, however. You will want to be familiar with JavaScript and HTML for most of the examples in the book. You’ll get introduced to a little C in the Arduino projects, but if you’re familiar with JavaScript or Java, it will look familiar enough. For those latter projects, you should be a little familiar with electronics, but you don’t have to be.

“What? I have to read other books in order to read this book?” No, but there are a few books that we found helpful in writing this one. We thought you might find them useful as well.

If you’re new to JavaScript, read Douglas Crockford’s JavaScript: The Good Parts. Come to think of it, read it if you’re an old hand at JavaScript. It’ll make you a better programmer. He explains the theoretical underpinnings of the language and the best use patterns clearly and definitively.

For PhoneGap and Android, the online Getting Started Guides are the most up-to-date references; see the PhoneGap developer portal and the Android developer site. For more in-depth introductions to Android, see Professional Android 4 Application Development or Android Programming: The Big Nerd Ranch Guide.

For an in-depth introduction to NFC from an engineering perspective, the NFC Forum Specifications are the original source material (we have reproduced some of them in Appendix A for handy reference). We also found Professional NFC Application Development by Vedat Coskun, Kerem Ok, and Busra Ozdenizci to be a good reference, particularly for experienced Java programmers. We have deliberately taken a more populist approach than that book, since many of our readers are hobbyists, hackers, and other self-identified dilettante programmers.

If you’re new to Arduino, Massimo Banzi’s Getting Started with Arduino is an excellent starting place. Tom Igoe’s Making Things Talk, 2nd Edition is a good book for experienced programmers to learn about connecting Arduino projects to networks. Michael Margolis’ Arduino Cookbook has some handy recipes for Arduino programs as well.

For an introduction to Node.js, which pops up later in this book, Brett McLaughlin’s What is Node? is a nice essay-length introduction with no code. Manuel Kiessling’s The Node Beginner Book and Pedro Teixeira’s Hands-On Node.js are helpful and short guides to getting started with the actual code.

For a good introduction to the Raspberry Pi or the BeagleBone Black, which you’ll encounter in Chapter 9, you can find material for getting started on Adafruit’s tutorials. The books Getting Started with Raspberry Pi by Matt Richardson and Shawn Wallace and Getting Started with BeagleBone by Matt Richardson are also good introductions.

What’s Covered in This Book

Chapter 2 gives you an introduction to near field communication (NFC) by comparing it to radio frequency identification (RFID). Simply put, NFC is a superset of RFID. It can do most things short-range RFID can do, and more. You’ll get a preview of the most important terms, a look at the architecture of an NFC system, and learn what tools you need and where to get them.

Chapter 3 introduces you to PhoneGap and the NFC plug-in for PhoneGap. You’ll install the tools necessary to develop PhoneGap applications for Android and build and run your first couple of applications. By the end of this chapter, you’ll have read your first NFC tag using an Android device.

Chapter 4 is an in-depth overview of the NFC Data Exchange Format (NDEF). You’ll learn how it’s structured and see it in practice by writing an application that performs the same basic task using different types of NDEF records, to see how each record type affects user interaction on Android.

Chapter 5 covers how to listen for NDEF messages on Android. You’ll learn how to filter for different types of tags and messages, and how the Android Tag Dispatch system can be used to your advantage when developing NFC apps.

In Chapter 6, you’ll build a full NFC application on Android that features a full user interface, audio playback, and control of web-connected lighting, all mediated by NFC tags. The goal of this chapter is to show you how to plan the interaction design and data formatting of an application to best take advantage of NFC.

Chapter 7 brings another platform into play: the Arduino microcontroller development platform. You’ll learn how to read and write NDEF messages using the Arduino NDEF library. You’ll also develop another full application using Arduino and Node.js.

Chapter 8 introduces you to peer-to-peer exchanges using NFC on Android. You’ll learn how the record types you’re exchanging through peer-to-peer affect the receiving device, and you’ll learn about how NFC can negotiate the handoff of larger exchanges to alternate carriers like Bluetooth and WiFi.

Chapter 9 gives you the state of the art on NFC development on embedded Linux platforms using the Raspberry Pi and BeagleBone as examples. You’ll get an understanding of what’s possible on embedded Linux, and see a few sample applications in Node.js. There’s still a lot of room for usability improvements in this context, so be warned that this chapter is not for the technically timid. You’ll want some familiarity with the Linux command-line interface to get the most out of this chapter. This is where some of the most exciting possibilities for NFC use lie, though, so it’s good territory to know.

What You’ll Need

To do the exercises in this book, you’ll need some hardware and software. All of the software is free, fortunately. The most expensive piece of hardware used here is an NFC-enabled Android device. The following sections list what you’ll be using.

Hardware

To follow along with the book overall, you’ll need the following hardware:

  • An Android NFC-enabled device
  • Several NFC-compatible tags (check compatibility with your devices; Device-to-Tag Type Matching includes a chart showing which devices work with which tag types)

For Chapter 6, you’ll need:

For Chapter 7, you’ll need:

For Chapter 9, you’ll need:

Table 1-1 lists the electronics components for this book with part numbers from some of the electronics distributors we use regularly, in case you want alternatives to those listed previously.

Table 1-1. Electronic components used in this book
Part MakerShed Jameco Digikey SparkFun Adafruit Farnell Arduino Seeed

220Ω resistor

690700

220QBK-ND

9337792

Solderless breadboard

MKEL3

20723

438-1045-ND

PRT-00137

64

4692810

STR101C2M or STR102C2M

Red hookup wire

MKSEEED3

36856

C2117R-100-ND

PRT-08023

1662031

Black hookup wire

MKSEEED3

36792

C2117B-100-ND

PRT-08022

1662027

Blue hookup wire

MKSEEED3

36767

1662034

12V 1000mA DC power supply (or equivalent)

170245

TOL-00298

798

636363

Arduino Uno rev3 Microcontroller Module

MKSP11

2121105

1050-1017-ND

DEV-09950

50

1848687

A000046

ARD132D2P

Green LED

MKEE7

333227

COM-09592

1334976

Red LED

MKEE7

333973

COM-09590

2062463

Blue LED

2006764

COM-00529

1020554

Yellow LED

MKEE7

34825

COM-09594

1939531

MIFARE RFID tag

MKPX4

SEN-10128

NFC shield

MKAD45

789

SLD01097P

TIP120 Darlington transistor

10001_​10001_​32993_-1

TIP120-ND

976

9294210

3.3V USB/TTL serial debug cable

DEV-09717

954

BeagleBone Black

MKCCE3

2176149

BB-BBLK-000-ND

1278

2291620

ARM00100P

Raspberry Pi Model B

MKRPI2

DEV-11546

998

43W5302

Software

To follow along with the book overall, you’ll need the following software:

For Chapter 6, you’ll need:

  • The Zepto jQuery library, available from Zepto.js

For Chapter 7, you’ll need:

Don’t worry about setting all this up right now; we’ll let you know when you need to install a piece of software.

Other Useful NFC Apps

The following will be useful throughout the course of the book:

  • NFC TagInfo by NXP allows you to read any NFC or Mifare tag and examine the NDEF record on it.
  • NFC TagWriter by NXP allows you to do many of the same things as TagInfo. It can also write tags, and unformat tags, which is really handy.
  • NFC Research Lab’s NFC TagInfo will show you all the info about a given tag. It’s more advanced than NXP’s TagInfo, in that it will also allow you to see a memory dump from the tag. It’s invaluable for troubleshooting your applications.

For Chapter 4, you’ll need:

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Tip

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/tigoe/beginningnfc.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Beginning NFC: Near Field Communication with Arduino, Android, and PhoneGap, by Tom Igoe, Don Coleman, and Brian Jepson. Copyright Tom Igoe, Don Coleman, and Brian Jepson 2014 978-1-4493-6307-9.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/beginning-nfc.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

We’ve received generous assistance from many people and organizations during the writing of this book. The PhoneGap-NFC plug-in was Kevin Griffin’s brainchild; he and Don wrote the first version of it and presented it at PhoneGap Day 2011. Kevin Townsend of Adafruit has been an invaluable resource for his in-depth knowledge of NXP’s software and hardware. Yihui Xiong of Seeed Studio and author of the Seeed Arduino NDEF library, was crucial to the success of Chapter 7. Philippe Teuwen made fast patches to libfreefare, clearing roadblocks in Chapter 9. Derek Molloy’s pages on the BeagleBone Black were a handy resource. The images in this book are better thanks to Jody Culkin and Fritzing.org. Early readers Ben Light, Sae Huh, Gabrielle Levine, Alex Kauffmann, Fil Maj, and Dominick Gruntz offered valuable feedback.

This book was made possible by the patient support of our employers and colleagues at NYU’s Interactive Telecommunications Program (ITP), Chariot Solutions LLC, Maker Media, and Arduino. We would also like to acknowledge the even more patient support of our families and partners.

Get Beginning NFC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.