web api instance method
NDEFReader: scan() method
Secure contextLimited availability
The scan() method of the NDEFReader interface activates a reading device and returns a Promise that either resolves when an NFC tag read operation is scheduled 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
scan(options)
Parameters
optionsOptional- : An object with the following properties:
signal- : An
AbortSignalthat allows cancelling thisscan()operation.
- : An
- : An object with the following properties:
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:
AbortErrorDOMException- : Returned if the scan operation was aborted with the
AbortSignalpassed in theoptionsargument.
- : Returned if the scan operation was aborted with the
InvalidStateErrorDOMException- : Returned if there’s already an ongoing scan.
NotAllowedErrorDOMException- : Returned if the permission for this operation was rejected.
NotSupportedErrorDOMException- : Returned if there is no NFC adapter compatible with Web NFC or a connection cannot 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
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.