NDEFReader.scan()

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 scan() method of the NDEFReader interface 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.

Syntax

var readerPromise = NDEFReader.scan(options);

Parameters

options Optional

An object with the following properties:

  • signal -- An AbortSignal that allows cancelling this scan() operation.

Return value

A Promise that resolves immediately after scheduling read operations for the NFC adapter.

Exceptions

This method doesn't throw exceptions; instead, it rejects the returned promise, passing a DOMException whose name is one of the following:

AbortError

The scan operation was aborted with the AbortSignal passed in the options argument.

InvalidStateError

There's already an ongoing scan.

NotAllowedError

The permission for this operation was rejected.

NotSupportedError

There is no NFC adapter compatible with Web NFC, or a connection can not be established.

Examples

Handle scanning errors

This example shows what happens when a scan promise rejects and readingerror is thrown.

const ndef = new NDEFReader();
ndef.scan().then(() => {
  console.log("Scan started successfully.");
  ndef.onreadingerror = (event) => {
    console.log("Error! Cannot read data from the NFC tag. Try a different one?");
  };
  ndef.onreading = (event) => {
    console.log("NDEF message read.");
  };
}).catch(error => {
  console.log(`Error! Scan failed to start: ${error}.`);
});

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
scan
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/scan