NDEFReader

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The NDEFReader interface of the Web NFC API is used to read from and write data to compatible NFC devices, e.g. NFC tags supporting NDEF, when these devices are within the reader's magnetic induction field.

Constructor

NDEFReader()

Returns a new NDEFReader object.

Properties

Inherits properties from its parent, EventTarget.

NDEFReader.onreading

An event handler called when the reading event is raised.

NDEFReader.onreadingerror

An event handler called when when the readingerror event is raised. This occurs when a tag is in proximity of a reading device, but cannot be read.

Methods

The NDEFReader interface inherits the methods of EventTarget, its parent interface.

NDEFReader.scan()

Activates a reading device and returns a Promise that either resolves when an NFC tag is read or rejects if a hardware or permission error is encountered. This method triggers a permission prompt if the "nfc" permission has not been previously granted.

NDEFReader.write()

Attempts to write an NDEF message to a tag and returns a Promise that either resolves when a message has been written to the tag or rejects if a hardware or permission error is encountered. This method triggers a permission prompt if the "nfc" permission has not been previously granted.

Examples

Handling initial reads while writing

The example below shows how to coordinate between a common reading handler and one used specifically for a single write. In order to write, a tag needs to be found and read. This gives you the ability to check whether it is actually a tag that you want to write to. That's why it's recommended that you call write() from a reading event.

const ndef = new NDEFReader();
let ignoreRead = false;

ndef.onreading = (event) => {
  if (ignoreRead) {
    return; // write pending, ignore read.
  }

  console.log("We read a tag, but not during pending write!");
};

function write(data) {
  ignoreRead = true;
  return new Promise((resolve, reject) => {
    ndef.addEventListener("reading", event => {
      // Check if we want to write to this tag, or reject.
      ndef.write(data).then(resolve, reject).finally(() => ignoreRead = false);
    }, { once: true });
  });
}

await ndef.scan();
try {
  await write("Hello World");
  console.log("We wrote to a tag!")
} catch(err) {
  console.error("Something went wrong", err);
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
NDEFReader
No
No
No
No
No
No
No
89
No
No
No
No
NDEFReader
No
No
No
No
No
No
No
89
No
No
No
No
onreading
No
No
No
No
No
No
No
89
No
No
No
No
onreadingerror
No
No
No
No
No
No
No
89
No
No
No
No
scan
No
No
No
No
No
No
No
89
No
No
No
No
secure_context_required
No
No
No
No
No
No
No
89
No
No
No
No
write
No
No
No
No
No
No
No
89
No
No
No
No

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/NDEFReader