WebAPI/WebNFC
< WebAPI
Jump to navigation
Jump to search
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, Siddartha Pothapragada, Philipp von Weitershausen