-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
82 lines (61 loc) · 2.28 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "NDEFReader: scan() method"
short-title: scan()
slug: Web/API/NDEFReader/scan
page-type: web-api-instance-method
status:
- experimental
browser-compat: api.NDEFReader.scan
---
{{SecureContext_Header}}{{SeeCompatTable}}{{APIRef("Web NFC API")}}
The `scan()` method of the {{DOMxRef("NDEFReader")}} interface activates a reading device and returns a {{jsxref("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
```js-nolint
scan(options)
```
### Parameters
- `options` {{optional_inline}}
- : An object with the following properties:
- `signal`
- : An {{DOMxRef("AbortSignal")}} that allows cancelling this `scan()` operation.
### Return value
A {{JSxRef("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 {{domxref("DOMException")}} whose `name` is one of the
following:
- `AbortError` {{domxref("DOMException")}}
- : Returned if the scan operation was aborted with the {{DOMxRef("AbortSignal")}} passed in the `options` argument.
- `InvalidStateError` {{domxref("DOMException")}}
- : Returned if there's already an ongoing scan.
- `NotAllowedError` {{domxref("DOMException")}}
- : Returned if the permission for this operation was rejected.
- `NotSupportedError` {{domxref("DOMException")}}
- : 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.
```js
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
{{Specifications}}
## Browser compatibility
{{Compat}}