WebAPI/WebNFC: Difference between revisions
< WebAPI
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
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 | 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. | ||
interface Nfc | interface Nfc | ||
Line 25: | Line 25: | ||
interface NfcNdefEvent : nsIDOMEvent | interface NfcNdefEvent : nsIDOMEvent | ||
{ | { | ||
readonly attribute NfcNDefRecords[] ndefRecords; | readonly attribute NfcNDefRecords[] ndefRecords; | ||
}; | }; | ||
NDEF records contain a bunch of metadata and a payload that is exposed as | NDEF records contain a bunch of metadata and a payload that is exposed as a string. | ||
interface NfcNdefRecord | interface NfcNdefRecord | ||
Line 43: | Line 36: | ||
readonly attribute DOMString type; | readonly attribute DOMString type; | ||
readonly attribute DOMString id; | readonly attribute DOMString id; | ||
readonly attribute DOMString | readonly attribute DOMString payload; | ||
}; | }; | ||
== Example == | == Example == | ||
navigator.mozNfc. | navigator.mozNfc.onndefdiscovered = function (event) { | ||
console.log("Discovered an NDEF message with " + event. | console.log("Discovered an NDEF message with " + event.ndefRecords.length + " records."); | ||
event. | event.ndefRecords.forEach(function (record) { | ||
console.log("Found a " + record.tnf + " record" + | console.log("Found a " + record.tnf + " record" + | ||
" of type " + record.type + | " of type " + record.type + | ||
" with ID " + record.id + | " with ID " + record.id + | ||
" and payload " + record. | " and payload " + record.payload); | ||
// Could dispatch event.message here to other web apps based on MIME type, URI, etc. | // Could dispatch event.message here to other web apps based on MIME type, URI, etc. | ||
}); | }); |
Revision as of 21:55, 26 April 2012
First iteration: NDEF
Scope
- Technologies:
- Focus on NDEF standard only for now
- Others (e.g. proprietary MIFARE) to be investigated later.
- Capabilities:
- Read-only for now
- Tag creation/simulation and two-way communication to be investigated later
- Implementation:
- NDEF-only API available on navigator.mozNfc.ndef object
- Discovered NDEF tags are automatically parsed and dispatched to content in the "tagdiscovered" event on navigator.mozNfc.ndef
- 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 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.
interface Nfc { attribute EventListener onndefdiscovered; };
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 DOMString tnf; readonly attribute DOMString type; readonly attribute DOMString id; readonly attribute DOMString payload; };
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. }); };
Implementation
- See bug 674741
- Engineers: Markus Neubrand, Arno Puder, Garner Lee, Philipp von Weitershausen