MozNFC.getNFCPeer()

The getNFCPeer() method of the MozNFC interface returns a Peer to Peer DOM object (MozNFCPeer) that can be used to transfer data to another NFC-enabled device.

Syntax

MozNFCPeer getNFCPeer(DOMString sessionId);

Example

var tnf     = 1;                                             // NFC Forum Well Known type
var type    = new Uint8Array(fromUTF8("U"));                 // URL type
var id      = new Uint8Array(fromUTF8(""));                  // id
var payload = new Uint8Array(fromUTF8("\u0003mozilla.org")); // URL data, with a record prefix 0x3 replacing http://
var ndefRecords = [new MozNDEFRecord(tnf, type, id, payload)];
var nfcdom = window.navigator.mozNfc;
nfcdom.onpeerready = function(event) {
  var nfcPeer = nfcdom.getNFCPeer(event.detail);  // 'event.detail' has session id.
  var req = nfcpeer.sendNDEF(ndefRecords);        // push NDEF message to other NFC device.
  req.onsuccess = function(e) {
    console.log("Successfully pushed P2P message");
  };
  req.onerror = function(e) {
    console.log("P2P push failed!");
  };
};

Parameters

sessionId: A DOMString representing a session id that uniquely identifies a peer for the session in which they are available. The sessionId is made available when the peerready event fires (mozNFc.onpeerready), under the detail property of the event (see var nfcPeer = nfcdom.getNFCPeer(event.detail); above).

Returns

A MozNFCPeer object.

Errors

If the sessionId is invalid or expired, getNFCPeer throws the error "Unable to create NFCPeer object, Reason:  Bad SessionToken."

Specifications

The NFC implementation in Gecko follows the NFC Forum specifications.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

No support

No support No support No support No support
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support No support No support 2.0 moz [1] No support No support No support

[1] Available in privileged apps as of Firefox OS 2.2; certified-only before that.

See also

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, fscholz, teoli, lgarner
 Last updated by: chrisdavidmills,