Skip to content

Commit 8569986

Browse files
committed
Add support for Web NFC API
1 parent 9d7e1b7 commit 8569986

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSGlobal
5+
6+
/** The [[NDEFMessage]] interface of the Web NFC API represents the content of an NDEF message that has been read from
7+
* or could be written to an NFC tag. An instance is acquired by calling the NDEFMessage() constructor or from the
8+
* NDEFReadingEvent.message property, which is passed to the reading event.
9+
*
10+
* @param records\
11+
* The records property of NDEFMessage interface represents a list of NDEFRecords present in the NDEF message.
12+
*/
13+
@js.native
14+
@JSGlobal
15+
class NDEFMessage(var records: js.Array[NDEFRecord] = js.native) extends js.Object
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSGlobal
5+
6+
/** The [[NDEFReader]] interface of the Web NFC API (https://developer.mozilla.org/en-US/docs/Web/API/Web_NFC_API) is
7+
* used to read from and write data to compatible NFC devices, e.g. NFC tags supporting NDEF, when these devices are
8+
* within the reader's magnetic induction field.
9+
*/
10+
@JSGlobal("NDEFReader")
11+
@js.native
12+
class NDEFReader() extends EventTarget {
13+
14+
/** Activates a reading device and returns a [[Promise]] that either resolves when an NFC tag read operation is
15+
* scheduled or rejects if a hardware or permission error is encountered. This method triggers a permission prompt if
16+
* the "nfc" permission has not been previously granted.
17+
*
18+
* @return
19+
* a Promise that resolves immediately after scheduling read operations for the NFC adapter.
20+
*/
21+
def scan(options: NDEFScanOptions = js.native): js.Promise[Any] = js.native
22+
23+
/** Attempts to write an NDEF message to a tag and returns a [[Promise]] that either resolves when a message has been
24+
* written to the tag or rejects if a hardware or permission error is encountered. This method triggers a permission
25+
* prompt if the "nfc" permission has not been previously granted.
26+
*
27+
* @param message
28+
* The message to be written, either a string object or literal, an ArrayBuffer, a TypedArray, a DataView, or an
29+
* array of records. A record has the following members:
30+
* @param options
31+
* An object with the following properties:
32+
*
33+
* @return
34+
* a Promise that either resolves when a message has been written to the tag or rejects if a hardware or permission
35+
* error is encountered.
36+
*/
37+
def write(message: NDEFRecord.Data, options: NDEFWriteOptions = js.native): js.Promise[String] = js.native
38+
39+
/** The reading event of the NDEFReader interface is fired whenever a new reading is available from compatible NFC
40+
* devices (e.g. NFC tags supporting NDEF) when these devices are within the reader's magnetic induction field.
41+
*/
42+
var onreading: js.Function1[NDEFReadingEvent, Any] = js.native
43+
44+
/** The readingerror event of the NDEFReader interface is fired whenever an error occurs during reading of NFC tags,
45+
* e.g. when tags leave the reader's magnetic induction field.
46+
*/
47+
var onreadingerror: js.Function1[Event, Any] = js.native
48+
}
49+
50+
@js.native
51+
trait NDEFScanOptions extends js.Object {
52+
53+
/** An AbortSignal that allows the current write operation to be canceled. */
54+
def `signal`: js.UndefOr[AbortSignal] = js.native
55+
}
56+
57+
@js.native
58+
trait NDEFWriteOptions extends js.Object {
59+
60+
/** A boolean value specifying whether or not existing records should be overwritten, if such exists. */
61+
def `overwrite`: Boolean = js.native
62+
63+
/** An AbortSignal that allows the current write operation to be canceled. */
64+
def `signal`: js.UndefOr[AbortSignal] = js.native
65+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.|
5+
6+
/** The NDEFReadingEvent interface of the Web NFC API represents events dispatched on new NFC readings obtained by
7+
* NDEFReader.
8+
*
9+
* @see
10+
* https://developer.mozilla.org/en-US/docs/Web/API/NDEFReadingEvent
11+
*/
12+
@js.native
13+
trait NDEFReadingEvent extends Event {
14+
15+
/** Returns an NDEFMessage object containing the received message. */
16+
var message: NDEFMessage = js.native
17+
18+
/** Returns the serial number of the device, which is used for anti-collision and identification, or an empty string
19+
* if no serial number is available.
20+
*/
21+
var serialNumber: String | "" = js.native
22+
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.scalajs.dom
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.|
5+
import scala.scalajs.js.annotation.JSGlobal
6+
7+
/** The [[NDEFRecord]] interface of the Web NFC API provides data that can be read from, or written to, compatible NFC
8+
* devices, e.g. NFC tags supporting NDEF.
9+
*/
10+
@js.native
11+
@JSGlobal
12+
class NDEFRecord extends js.Object {
13+
14+
/** Returns the record type of the record. Records must have either a standardized well-known type name such as
15+
* "empty", "text", "url", "smart-poster", "absolute-url", "mime", or "unknown" or else an external type name, which
16+
* consists of a domain name and custom type name separated by a colon (":").
17+
*/
18+
var recordType: String = js.native
19+
20+
/** Returns the MIME type of the record. This value will be null if recordType is not equal to "mime". */
21+
var mediaType: js.UndefOr[String] = js.native
22+
23+
/** Returns the record identifier, which is an absolute or relative URL used to identify the record.
24+
*
25+
* Note: The uniqueness of the identifier is enforced only by the generator of the record.
26+
*/
27+
var id: js.UndefOr[String] = js.native
28+
29+
/** Returns a DataView containing the raw bytes of the record's payload. */
30+
var data: NDEFRecord.Data = js.native
31+
// var data: DataView = js.native
32+
33+
/** Returns the encoding of a textual payload, or null otherwise. */
34+
var encoding: js.UndefOr[String] = js.native
35+
36+
/** Returns the language of a textual payload, or null if one was not supplied. */
37+
var lang: js.UndefOr[String] = js.native
38+
39+
/** Converts [[NDEFRecord.data]] to a sequence of records. This allows parsing the payloads of record types which may
40+
* contain nested records, such as smart poster and external type records.
41+
*/
42+
def toRecords(): js.Array[NDEFRecord] = js.native
43+
}
44+
45+
object NDEFRecord {
46+
47+
type Data = String |
48+
js.typedarray.ArrayBuffer |
49+
// js.typedarray.TypedArray[NDEFRecord, ???] |
50+
js.typedarray.DataView |
51+
js.Array[NDEFRecord]
52+
}

0 commit comments

Comments
 (0)