Beginning NFC with PhoneGap & Arduino

O'Reilly Webcast April 29, 2014

Tom Igoe, Brian Jepson & Don Coleman

What is NFC?

What can I do with NFC?

  • Mobile Payments
  • Sharing Data between Phones
  • Pair with Bluetooth Device
  • Mass Transit Card
  • Smart Poster


Tags hold a tiny amount of data

Think bytes

not kilobytes, megabytes, or gigabytes

Each tag type

  • Stores data in different binary format
  • Has different security features
  • Has different ways to read and write

Common properties

  • UID
  • Type
  • Technology
  • Capacity
  • Is Read Only
  • Is Lockable


NFC Data Exchange Format

NDEF Message

NDEF Record

NDEF Record

  • TNF (Type Name Format)
  • Type
  • Payload
  • Record Id


Data for the user application

TNF - Type Name Format

Indicates the structure of the value of the type field

TNF Constants

  • Empty 0x0
  • Well Known 0x1
  • Mime Media 0x2
  • External 0x4


Identifier describing the type of the Payload

Must follow rules implied by TNF

Text Record

hello, world

  • TNF = Well Known
  • Type = T
  • Payload = 2 + en + hello, world

URI Record

  • TNF = Well Known
  • Type = U
  • Payload = 0x3 +

Mime Media Record

{ "message": "hello, world" }

  • TNF = Mime Media
  • Type = application/json
  • Payload = { "message": "hello, world" }

Helper Methods

  • ndef.textRecord("hello, world");
  • ndef.uriRecord("");
  • ndef.mimeMediaRecord("text/json", '{...}');

Quick Review...

NDEF Message

One or more NDEF Records

NDEF Record

Contains a payload of data

Information describing the payload

Adafruit NFC Shield

Seeed Studio NFC Shield

PN532 Library Based on Adafruit's Library and modified by Yihui Xiong to support both I2C and SPI

NDEF Library

for Arduino




Write Tag

Read Tag

Cordova NFC Plugin
$ cordova create nfc com.example.nfc NFC

$ cd nfc

$ cordova platform add android

$ cordova plugin add \

Add a Listener


Add a Listener


Handle the Event

onNfc: function(nfcEvent) {
    var tag = nfcEvent.tag,
        ndefMessage = tag.ndefMessage;


Decode the Payload

  util.isType(record, tnf, type);

if (util.isType(rec, TNF_WELL_KNOWN, RTD_URI)) {    
  value = ndef.uriHelper.decodePayload(payload);
} else if (util.isType(rec, TNF_WELL_KNOWN, RTD_TEXT)) {
  value = ndef.textHelper.decodePayload(payload);
} else if // ... 

Launching your App

  <action android:name=
  <data android:mimeType="text/plain"/>
  <category android:name=

Cordova NFC Plugin

  • Read message from tag or peer
  • Decode payload using TNF and Type
  • Write message to a tag
  • Share a message with a peer
  • Use handover for large messages
  • Launch app when scanning a tag

Technical Specs

Thank You

Tom Igoe

Brian Jepson

Don Coleman




Creative Commons License
Writing NFC Apps with Apache Cordova by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at