WebAPI/WebNFC: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
Line 1: Line 1:
== Preliminary scope ==
== Preliminary scope ==


* explicitly NDEF-only for now (navigator.mozNfc.ndef), other technologies to be investigated
* NDEF-only for now, other technologies to be investigated. Explicitly implementing NDEF-only API in navigator.mozNfc.ndef
* read-only, no tag creation
* read-only, no tag creation
* dispatch all discovered NDEF tags to the "tagdiscovered" event, privileged web app does filtering and dispatches e.g. via WebIntents.
* dispatch all discovered NDEF tags to the "tagdiscovered" event, privileged web app does filtering and dispatches e.g. via WebIntents.

Revision as of 23:02, 2 February 2012

Preliminary scope

  • NDEF-only for now, other technologies to be investigated. Explicitly implementing NDEF-only API in navigator.mozNfc.ndef
  • read-only, no tag creation
  • dispatch all discovered NDEF tags to the "tagdiscovered" event, privileged web app does filtering and dispatches e.g. via WebIntents.

Example

 navigator.mozNfc.ndef.ontagdiscovered = function (event) {
   console.log("Discovered an NDEF message with " + event.message.records.length + " records.");
   event.message.records.forEach(function (record) {
     console.log("Found a " + record.tnf + " record" +
                 " of type " + record.type +
                 " with ID " + record.id +
                 " and payload " + record.payloadText);
     // record.payloadArrayBuffer is an typed array representation of the payload
   });
 };

Proposed API

navigator.mozNfc returns an object that, for now, only exposed NDEF capabilities:

 interface Nfc
 {
   readonly attribute Ndef ndef;
 };

The NDEF capabilities are, for now, restricted to tag discovery. The "tagdiscovered" NfcNdefEvent will be fired when a new NDEF tag is discovered. The event will contain a reference to the NdefMessage object.

 interface NfcNdef : EventTarget
 {
   attribute EventListener ontagdiscovered;
 };
 
 interface NfcNdefEvent : nsIDOMEvent
 {
   readonly attribute NdefMessage message;
 };

An NDEF message is merely a container for NDEF records:

 interface NdefMessage
 {
   readonly attribute NDefRecords[] ndefRecords;  
 };

NDEF records contain a bunch of metadata and a payload that is exposed as both a string and a Typed Array buffer (akin to the response on XMLHttpRequest).

 interface NdefRecord 
 {
   readonly attribute DOMString tnf;
   readonly attribute DOMString type;
   readonly attribute DOMString id;
   readonly attribute DOMString payloadText;
   readonly attribute ArrayBuffer payloadArrayBuffer;  
 };

Implementation

  • See bug 674741
  • Engineers: Markus Neubrand, Arno Puder, Philipp von Weitershausen