WebAPI/WebNFC: Difference between revisions

Line 95: Line 95:
== NDEF Read Example ==
== NDEF Read Example ==


   navigator.mozNfc.onndefdiscovered = function (event) {
   navigator.mozNfc.ontechdiscovered = function (event) {
     console.log("Discovered an NDEF message with " + event.ndefRecords.length + " records.");
 
     event.ndefRecords.forEach(function (record) {
    var tech = event.message.content.tech;
      console.log("Found a " + record.tnf + " record" +
 
     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.
        // Could dispatch event.message here to other web apps based on MIME type, URI, etc.
    });
      });
   };
    }
   }


== NDEF Write Tag Example ==
== NDEF Write Tag Example ==
Confirmed users
153

edits