Confirmed users
153
edits
Line 95: | Line 95: | ||
== NDEF Read Example == | == NDEF Read Example == | ||
navigator.mozNfc. | navigator.mozNfc.ontechdiscovered = function (event) { | ||
var tech = event.message.content.tech; | |||
for (var i in tech) { | |||
if (tech[i] == 'NDEF') { | |||
var req = navigator.mozNfc.ndefRead(); | |||
req.onsuccess = function(e) { | |||
// NDEF Message with array of NDEFRecords | |||
var records = e.target.result; | |||
handleNdefDiscovered(records); | |||
}; | |||
req.onerror = function() { | |||
console.log('ERROR: Failed to read NDEF on tag.'); | |||
}; | |||
} | |||
} | |||
function handleNdefDiscovered(records) { | |||
records.forEach(function (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.payload); | " and payload " + record.payload); | ||
// Could dispatch event.message here to other web apps based on MIME type, URI, etc. | |||
}); | |||
} | } | ||
} | |||
== NDEF Write Tag Example == | == NDEF Write Tag Example == |