6
6
- Francois Beaufort
7
7
8
8
Participate
9
- - https://github.com/w3c/web-nfc/issues/
9
+ - https://github.com/w3c/web-nfc/issues/
10
10
- https://lists.w3.org/Archives/Public/public-web-nfc/
11
11
12
12
## Introduction
@@ -92,17 +92,17 @@ when required, about granting access to Web NFC. It means sending and receiving
92
92
info when users tap NFC devices can be done smoothly once permission is granted.
93
93
94
94
``` js
95
- const reader = new NDEFReader ();
96
-
95
+ const ndef = new NDEFReader ();
96
+
97
97
async function startScanning () {
98
- await reader .scan ();
99
- reader .onreading = event => {
98
+ await ndef .scan ();
99
+ ndef .onreading = event => {
100
100
/* handle NDEF messages */
101
101
};
102
102
}
103
-
103
+
104
104
const nfcPermissionStatus = await navigator .permissions .query ({ name: " nfc" });
105
- if (permissionStatus .state === " granted" ) {
105
+ if (nfcPermissionStatus .state === " granted" ) {
106
106
// NFC access was previously granted, so we can start NFC scanning now.
107
107
startScanning ();
108
108
} else {
@@ -123,13 +123,13 @@ USB](https://wicg.github.io/webusb/) in that it offers secure low level access
123
123
with no magic and instead exposes data as a DataView as well as requires text to
124
124
be encoded and decoded using TextEncoder/TextDecoder, which allows handling
125
125
cases where the text might even be in UTF-16 do to existing real life
126
- implementations.
126
+ implementations.
127
127
128
128
``` js
129
- const reader = new NDEFReader ();
129
+ const ndef = new NDEFReader ();
130
130
131
- await reader .scan ({ recordType : " example.com:smart-poster " } );
132
- reader .onreading = event => {
131
+ await ndef .scan ();
132
+ ndef .onreading = event => {
133
133
const externalRecord = event .message .records .find (
134
134
record => record .type == " example.com:smart-poster"
135
135
);
@@ -162,13 +162,12 @@ const abortController = new AbortController();
162
162
abortController .signal .onabort = event => {
163
163
// All NFC operations have been aborted.
164
164
};
165
-
166
- const reader = new NDEFReader ();
167
- await reader .scan ({ signal: abortController .signal });
168
-
169
- const writer = new NDEFWriter ();
170
- await writer .push (" foo" , { signal: abortController .signal });
171
-
165
+
166
+ const ndef = new NDEFReader ();
167
+ await ndef .scan ({ signal: abortController .signal });
168
+
169
+ await ndef .write (" foo" , { signal: abortController .signal });
170
+
172
171
document .querySelector (" #abortButton" ).onclick = event => {
173
172
abortController .abort ();
174
173
};
@@ -228,13 +227,11 @@ same API, and have it loaded as a separate module. People working on Web
228
227
Assembly are also advocating for this patterns as it might be able to turn such
229
228
globals into modules in the future, at least when accessed from WASM.
230
229
231
- ### Separate objects for reader/writer
230
+ ### Common vs separate objects for reader/writer
231
+
232
+ The reader and writer objects existed separately when scan filters were supported,
233
+ but after filters have been abandoned, they have been merged into one single object.
232
234
233
- The reader and writer objects could have been merged into one single object, but
234
- as we allow multiple scans with filters to be active at the same time (maybe in
235
- multiple places/view of the app/site) then it makes more sense to be able to use
236
- separate objects.
237
-
238
235
## Considered alternatives
239
236
240
237
### Restrict to NDEF and name accordingly
0 commit comments