WebAPI/WebNFC: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:
** Others (e.g. proprietary MIFARE) to be investigated later.
** Others (e.g. proprietary MIFARE) to be investigated later.
* Capabilities:
* Capabilities:
** Read-only for now
** Read/write NDEF records on tags
** Tag creation/simulation and two-way communication to be investigated later
** P2P NDEF push/receive
* Implementation:
* Implementation:
** NDEF-only API available on navigator.mozNfc.ndef object
** NDEF-only API available on navigator.mozNfc object
** Discovered NDEF tags are automatically parsed and dispatched to content in the "tagdiscovered" event on navigator.mozNfc.ndef
** Discovered NDEF tags are automatically parsed and dispatched to content in the "ndefdiscovered" event on navigator.mozNfc
** navigator.mozNfc only available to a specific privileged content page (cf. WebTelephony, WebSMS).
** navigator.mozNfc only available to a specific privileged content page (cf. WebTelephony, WebSMS).
** For now, content is expected to do filtering and dispatching to handlers e.g. via WebIntents/WebActions/postMessage
** For now, content is expected to do filtering and dispatching to handlers e.g. via WebIntents/WebActions/postMessage
Line 17: Line 17:
== Proposed API ==
== Proposed API ==


navigator.mozNfc for now has only one event restricted to NDEF tag discovery. The "ndefdiscovered" NfcNdefEvent will be fired when a new NDEF tag is discovered. The event will contain an array of NfcNdefRecords contained on the tag.
navigator.mozNfc has one event restricted to NDEF discovery. The "ndefdiscovered" NfcNdefEvent will be fired when a new NDEF message is discovered either via reading a tag or receiving it via P2P communication. The event will contain an array of the received NfcNdefRecords. There are two functions to write NDEF records either to a tag or push them as P2P message.


   interface Nfc
   interface Nfc
   {
   {
     attribute EventListener onndefdiscovered;
     attribute EventListener onndefdiscovered;
     attribute EventListener ontaglost;
     attribute EventListener onndefdisconnected;
 
    nsIDOMDOMRequest writeNdefTag(in NfcNDefRecords[] records);
    nsIDOMDOMRequest ndefPush(in NfcNDefRecords[] records);
   };
   };


Line 34: Line 37:
   interface NfcNdefRecord  
   interface NfcNdefRecord  
   {
   {
     readonly attribute DOMString tnf;
     readonly attribute octet tnf;
     readonly attribute DOMString type;
     readonly attribute DOMString type;
     readonly attribute DOMString id;
     readonly attribute DOMString id;
Line 40: Line 43:
   };
   };


== NDef Read Example ==
== NDEF Read Example ==


   navigator.mozNfc.onndefdiscovered = function (event) {
   navigator.mozNfc.onndefdiscovered = function (event) {
Line 52: Line 55:
     });
     });
   };
   };
== NDEF Write Tag Example ==
  navigator.mozNfc.onndefdiscovered = function (event) {
    var ndefRecords = [ new MozNdefRecord(1, "U", "", "\u0000http://mozilla.org") ];
    var domreq = navigator.mozNfc.writeNdefTag(ndefRecords);
    domreq.onsuccess = function(e) {
      console.log("Successfully wrote records to tag");
    }
    domreq.onerror = function(e) {
      console.log("Write failed!");
    }
  };
== NDEF P2P Push Example ==
  var ndefRecords = [ new MozNdefRecord(1, "U", "", "\u0000http://mozilla.org") ];
  var domreq = navigator.mozNfc.ndefPush = function (event) {
  domreq.onsuccess = function(e) {
    console.log("Successfully pushed P2P message");
  }
  domreq.onerror = function(e) {
    console.log("P2P push failed!");
  }


== Implementation ==
== Implementation ==


* See {{bug|674741}}
* See {{bug|674741}}
* Engineers: Markus Neubrand, Arno Puder, Garner Lee, Philipp von Weitershausen
* Engineers: Markus Neubrand, Arno Puder, Garner Lee, Siddarta Pothapragada, Philipp von Weitershausen

Revision as of 19:27, 8 November 2012

First iteration: NDEF

Scope

  • Technologies:
    • Focus on NDEF standard only for now
    • Others (e.g. proprietary MIFARE) to be investigated later.
  • Capabilities:
    • Read/write NDEF records on tags
    • P2P NDEF push/receive
  • Implementation:
    • NDEF-only API available on navigator.mozNfc object
    • Discovered NDEF tags are automatically parsed and dispatched to content in the "ndefdiscovered" event on navigator.mozNfc
    • navigator.mozNfc only available to a specific privileged content page (cf. WebTelephony, WebSMS).
    • For now, content is expected to do filtering and dispatching to handlers e.g. via WebIntents/WebActions/postMessage

Proposed API

navigator.mozNfc has one event restricted to NDEF discovery. The "ndefdiscovered" NfcNdefEvent will be fired when a new NDEF message is discovered either via reading a tag or receiving it via P2P communication. The event will contain an array of the received NfcNdefRecords. There are two functions to write NDEF records either to a tag or push them as P2P message.

 interface Nfc
 {
   attribute EventListener onndefdiscovered;
   attribute EventListener onndefdisconnected;
 
   nsIDOMDOMRequest writeNdefTag(in NfcNDefRecords[] records);
   nsIDOMDOMRequest ndefPush(in NfcNDefRecords[] records);
 };
 interface NfcNdefEvent : nsIDOMEvent
 {
   readonly attribute NfcNDefRecords[] ndefRecords;  
 };

NDEF records contain a bunch of metadata and a payload that is exposed as a string.

 interface NfcNdefRecord 
 {
   readonly attribute octet tnf;
   readonly attribute DOMString type;
   readonly attribute DOMString id;
   readonly attribute DOMString payload;
 };

NDEF Read Example

 navigator.mozNfc.onndefdiscovered = function (event) {
   console.log("Discovered an NDEF message with " + event.ndefRecords.length + " records.");
   event.ndefRecords.forEach(function (record) {
     console.log("Found a " + record.tnf + " record" +
                 " of type " + record.type +
                 " with ID " + record.id +
                 " and payload " + record.payload);
     // Could dispatch event.message here to other web apps based on MIME type, URI, etc.
   });
 };

NDEF Write Tag Example

 navigator.mozNfc.onndefdiscovered = function (event) {
   var ndefRecords = [ new MozNdefRecord(1, "U", "", "\u0000http://mozilla.org") ];
   var domreq = navigator.mozNfc.writeNdefTag(ndefRecords);
   domreq.onsuccess = function(e) {
     console.log("Successfully wrote records to tag");
   }
   domreq.onerror = function(e) {
     console.log("Write failed!");
   }
 };

NDEF P2P Push Example

 var ndefRecords = [ new MozNdefRecord(1, "U", "", "\u0000http://mozilla.org") ];
 var domreq = navigator.mozNfc.ndefPush = function (event) {
 domreq.onsuccess = function(e) {
   console.log("Successfully pushed P2P message");
 }
 domreq.onerror = function(e) {
   console.log("P2P push failed!");
 }

Implementation

  • See bug 674741
  • Engineers: Markus Neubrand, Arno Puder, Garner Lee, Siddarta Pothapragada, Philipp von Weitershausen