NFC in Firefox OS

Firefox OS is being developed in an open collaboration with Mozilla’s partners and community. In that spirit, and over the course of over a year, Mozilla and Deutsche Telekom (DT) teams worked closely together to develop a platform-level support for NFC within Firefox OS. During that time, both teams had regular product and engineering meet-ups for the end-to-end development cycle.

From proposing the NFC API, to defining the overall architecture, to prototyping and completing a production-level implementation on shipping products, this collaboration model worked so well that it really helped showcase the power of the “open” (Open technology and Open contribution model) for pushing the web forward. After all, this is exactly what Mozilla and Firefox OS stand for.

In this post, we describe a few basics around Firefox OS NFC implementation.

NFC Roadmap

Currently in release 2.0, Firefox OS supports NFC based sharing of contents (contacts, images, videos, URLs), as well as wirelessly reading information stored in NFC enabled tags (tag reading). Our sharing use cases are compatible with NFC enabled devices from other OSes like Android/Windows, so sharing of these contents across these devices would work. Our NFC API (first introduced in v1.3) has been put to use for these sharing use cases in v2.0 with core apps.

The Overall B2G roadmap is available on the wiki.

WebNFC API

The Firefox NFC APIs allow Peer to Peer (P2P) communication between any 2 devices that support NFC Data Type Exchange format (NDEF). NFC passive tags that can present themselves as NDEF compatible tags can also be read and written to. Firefox OS’ NFC implementation is currently for certified applications only, but as stated above, will be opened to marketplace applications as the API is developed to cover more use cases and data formats.

An example using this API

The following does P2P communications between 2 NFC devices (from the NFC API docs on MDN):

// Utility Function for UTF-8 string conversion to Uint8Array.
// Or ideally, simply add this to your webapp HTML to use NfcUtils:
// <script defer src="shared/js/nfc_utils.js"></script>
function fromUTF8(str) {
  if (!str) {
    return null;
  }
  var enc = new TextEncoder('utf-8');
  return enc.encode(str);
}
 
var tnf     = 1;                                             // NFC Forum Well Known type
var type    = new Uint8Array(fromUTF8("U"));                 // URL type
var id      = new Uint8Array(fromUTF8(""));                  // id
var payload = new Uint8Array(fromUTF8("u0003mozilla.org")); // URL data, with a record prefix 0x3 replacing http://
 
var ndefRecords = [new MozNDEFRecord(tnf, type, id, payload)];
var nfcdom = window.navigator.mozNfc;
 
nfcdom.onpeerready = function(event) {
  // event.detail is a session token
  var nfcPeer = navigator.mozNfc.getNFCPeer(event.detail);
  var req = nfcpeer.sendNDEF(ndefRecords); // push NDEF message to other NFC device.
  req.onsuccess = function(e) {
    console.log("Successfully pushed P2P message");
  };
  req.onerror = function(e) {
    console.log("P2P push failed!");
  };
};

More such examples that ship with Firefox OS can be found in Using the NCF API.

Current Supported data types

The WebNFC API currently supports NFC Data Exchange Format (NDEF). There are some future plans for Non-NDEF types. From the example above, it is 4 fields, which is defined with 3 optional Uint8Array data types. The TNF and type are used to route the message to the appropriate registered web application(s).

(Source: http://git.mozilla.org/?p=releases/gecko.git;a=blob_plain;f=dom/webidl/MozNDEFRecord.webidl;hb=refs/heads/v2.0)

[Constructor(octet tnf, optional Uint8Array type, optional Uint8Array id, optional Uint8Array payload)]
interface MozNDEFRecord
{
  /**
   * Type Name Field (3-bits) - Specifies the NDEF record type in general.
   *   tnf_empty: 0x00
   *   tnf_well_known: 0x01
   *   tnf_mime_media: 0x02
   *   tnf_absolute_uri: 0x03
   *   tnf_external type: 0x04
   *   tnf_unknown: 0x05
   *   tnf_unchanged: 0x06
   *   tnf_reserved: 0x07
   */
  [Constant]
  readonly attribute octet tnf;
 
  /**
   * type - Describes the content of the payload. This can be a mime type.
   */
  [Constant]
  readonly attribute Uint8Array? type;
 
  /**
   * id - Identifier is application dependent.
   */
  [Constant]
  readonly attribute Uint8Array? id;
 
  /**
   * payload - Binary data blob. The meaning of this field is application
   * dependent.
   */
  [Constant]
  readonly attribute Uint8Array? payload;
};

Note, in upcoming Firefox OS releases, we will be updating the data types slightly to make TNF an enum type instead of an octet.

Mozilla’s Flame device supports NFC, more devices coming

Our Flame device supports NFC and we are expecting more commercial devices from our partners soon. Flame device supports NFC chipset from NXP (PN547C2).

Videos

Here is a demo video of some of the NFC sharing features based on Firefox OS:

Core Apps In Flame device that use NFC:

  • Gallery
  • Video
  • Music
  • Settings
  • System browser

A sample 3rd party App

Here is an app that Mozillian Dietrich Ayala put together using the NFC tag reading API. BikeCommute is an app that registers an NFC tag to track bike commuters at the Mozilla Portland office. The app is running on a Nexus 4 with Firefox OS 2.2, and is built with Famo.us for UI and PouchDB for data storage and syncing to a remote CouchDB. Currently, the app just reads the user’s email address from a text record written to the tag.

The next version will add support for running the app on users’ phones, using a local contact (user) instead of a plain text record, and being able to configure the NFC tag from their own device. The plan is to develop leaderboards from the CouchDB data and Mozillians.org integration so we can deploy and compete with other offices and Mozillians everywhere! The source code is available on GitHub and pull requests welcome!

Here is a Video demo of this app in action:

More NFC documentation

So, there it is!

We are really excited to introduce this new addition to growing list of APIs and features in Firefox OS! We hope developers will take full advantage of all that NFC enables by way of device-to-device sharing and also services like contactless payment planned in future.

When can developers start using this API?

Currently this API is available for certified apps. We can’t wait to finish the work to make this API available for privileged apps, so all of you developers can take advantage of this. If you wish to follow along or jump in and help out, feel free to track Bug 1042851. We are targeting to finish the work for the next release v2.2.

Next in NFC

In upcoming releases, with the help of our partners, we are focusing on expanding the NFC coverage for supporting Secure elements and services like NFC based payments. More on that in a separate post later. Please stay tuned.

Here’s to the open web!

About Robert Nyman [Editor emeritus]

Technical Evangelist & Editor of Mozilla Hacks. Gives talks & blogs about HTML5, JavaScript & the Open Web. Robert is a strong believer in HTML5 and the Open Web and has been working since 1999 with Front End development for the web - in Sweden and in New York City. He regularly also blogs at http://robertnyman.com and loves to travel and meet people.

More articles by Robert Nyman [Editor emeritus]…

About Sandip Kamat

Sandip Kamat is part of Mozilla's Connected Devices Product Management team. He has spent most of his career in building mobile technologies and products. Prior to joining Mozilla, he worked at Motorola Mobility (later, owned by Google) and Siemens Mobile. He is an alum of IIT Madras and UCSD (Rady). He is passionate about bringing cutting edge technologies to everyday people to make their lives meaningfully better.

More articles by Sandip Kamat…


9 comments

  1. Steve Lee

    I can’t edit compatibility doc to add comments but wanted to say having just investigated NFC tags in detail for the http://gpii.net RFID listener [1] we have moved from the MiFair Classic 1K to NTAG2x3 family (eg NFC203, but NFC213 appears to be the most sensitive). Currently NTAG seem to be the universally most popular and well supported tags. I found it hard to get hold of any good clear open documentation – largely as they don’t really seem to follow the ISO 7 layer model – so it gets very confusing.

    [1] https://github.com/OpenDirective/GPII-windows/releases/tag/Listeners_V1.3.0

    November 10th, 2014 at 11:42

    1. Sandip Kamat

      Steve, Thanks for that info. Feel free to request access to that doc for adding comments. Thanks, Sandip

      November 10th, 2014 at 13:22

    2. Garner Lee

      As long as the particular NTAG213 can expose functionality as a standard NDEF (NFC Data Exchange Format) compatible tag, it should be usable as an NDEF tag. The basic NDEF format (tnf, type, id, payload) support *should* keep things interoperable.

      Are you porting your code (Node.js?) to FirefoxOS?

      November 10th, 2014 at 16:59

    3. Garner Lee

      As long as the particular NTAG213 can expose functionality as a standard NDEF (NFC Data Exchange Format) compatible tag, it should be usable as an NDEF tag. The basic NDEF format support (tnf, type, id, payload) *should* keep things interoperable.

      Are you porting your code (Node.js?) to FirefoxOS?

      November 10th, 2014 at 17:01

  2. Steve Lee

    Sandip: who would I ask?

    Garner: yes are NFC forum type 2 compatible so NDEF supported

    We have user listeners for windows, linux and Android as we are EC funded for those. As I’m involved with FxOS I’m keen to port the core arcitecture to it, at least those components that make sense. Yes it’s node and I was thinking about trying browserify

    November 11th, 2014 at 02:41

    1. Sandip Kamat

      Steve, you can just click “request access” from the G-doc, so doc owners can receive the request with the account you would like to use. Thx

      November 11th, 2014 at 18:58

  3. Moinuddin Chinu

    I want to know that how can I use in Bangoli lengues in my this phone .please help me.

    November 13th, 2014 at 08:00

  4. Charles

    Will the NFC API eventually support sending and receiving raw APDU commands?

    November 18th, 2014 at 04:29

    1. Garner Lee

      There’s some initial ideas for a new tag object to handle “transceive(something)”, but nothing exactly definitive yet.

      What application and support do you have in mind?

      November 19th, 2014 at 14:51

Comments are closed for this article.