diff --git a/EXPLAINER.md b/EXPLAINER.md index 764f67b..e1d3c73 100644 --- a/EXPLAINER.md +++ b/EXPLAINER.md @@ -6,7 +6,7 @@ Authors: - Francois Beaufort Participate -- https://github.com/w3c/web-nfc/issues/ +- https://github.com/w3c/web-nfc/issues/ - https://lists.w3.org/Archives/Public/public-web-nfc/ ## Introduction @@ -92,17 +92,17 @@ when required, about granting access to Web NFC. It means sending and receiving info when users tap NFC devices can be done smoothly once permission is granted. ```js -const reader = new NDEFReader(); - +const ndef = new NDEFReader(); + async function startScanning() { - await reader.scan(); - reader.onreading = event => { + await ndef.scan(); + ndef.onreading = event => { /* handle NDEF messages */ }; } - + const nfcPermissionStatus = await navigator.permissions.query({ name: "nfc" }); -if (permissionStatus.state === "granted") { +if (nfcPermissionStatus.state === "granted") { // NFC access was previously granted, so we can start NFC scanning now. startScanning(); } else { @@ -123,13 +123,13 @@ USB](https://wicg.github.io/webusb/) in that it offers secure low level access with no magic and instead exposes data as a DataView as well as requires text to be encoded and decoded using TextEncoder/TextDecoder, which allows handling cases where the text might even be in UTF-16 do to existing real life -implementations. +implementations. ```js -const reader = new NDEFReader(); +const ndef = new NDEFReader(); -await reader.scan({ recordType: "example.com:smart-poster" }); -reader.onreading = event => { +await ndef.scan(); +ndef.onreading = event => { const externalRecord = event.message.records.find( record => record.type == "example.com:smart-poster" ); @@ -162,13 +162,12 @@ const abortController = new AbortController(); abortController.signal.onabort = event => { // All NFC operations have been aborted. }; - -const reader = new NDEFReader(); -await reader.scan({ signal: abortController.signal }); - -const writer = new NDEFWriter(); -await writer.push("foo", { signal: abortController.signal }); - + +const ndef = new NDEFReader(); +await ndef.scan({ signal: abortController.signal }); + +await ndef.write("foo", { signal: abortController.signal }); + document.querySelector("#abortButton").onclick = event => { abortController.abort(); }; @@ -228,13 +227,11 @@ same API, and have it loaded as a separate module. People working on Web Assembly are also advocating for this patterns as it might be able to turn such globals into modules in the future, at least when accessed from WASM. -### Separate objects for reader/writer +### Common vs separate objects for reader/writer + +The reader and writer objects existed separately when scan filters were supported, +but after filters have been abandoned, they have been merged into one single object. -The reader and writer objects could have been merged into one single object, but -as we allow multiple scans with filters to be active at the same time (maybe in -multiple places/view of the app/site) then it makes more sense to be able to use -separate objects. - ## Considered alternatives ### Restrict to NDEF and name accordingly diff --git a/index.html b/index.html index 7bddc51..077e8e0 100644 --- a/index.html +++ b/index.html @@ -9,8 +9,9 @@ 'remove'>