Skip to content

Instantly share code, notes, and snippets.

@richardszalay
Created April 8, 2019 10:51
Show Gist options
  • Save richardszalay/dd39a3486b6477a126103c6ea354247b to your computer and use it in GitHub Desktop.
Save richardszalay/dd39a3486b6477a126103c6ea354247b to your computer and use it in GitHub Desktop.
Types for WebNFC as implemented by Chrome
/**
* Types for the version of Web NFC available in Chrome for Android
*
* The spec is similar to, but does not exactly match https://w3c.github.io/web-nfc/releases/20151112/
*/
declare global {
interface Navigator {
nfc: WebNFC
}
interface WebNFC {
watch(messageScanned: MessageCallback, options?: NFCWatchOptions): Promise<number>
cancelWatch(id?: number): Promise<void>;
push(message: NFCPushMessage): Promise<void>;
cancelPush(target?: NFCPushTarget): Promise<void>;
}
interface NFCMessage {
records: NFCRecord[],
url: string
}
interface NFCRecord {
data: NFCRecordData,
mediaType: string,
recordType: NFCRecordType
}
type NFCRecordData = string | number | ArrayBuffer | object;
interface MessageCallback {
(message: NFCMessage): void
}
interface NFCWatchOptions {
url?: string,
kind?: NFCRecordType,
mediaType?: string,
mode?: NFCWatchMode
}
type NFCWatchMode =
"web-nfc-only" |
"any";
type NFCRecordType =
"empty" |
"text" |
"url" |
"json" |
"opaque";
type NFCPushMessage =
string |
ArrayBuffer |
NFCMessage;
interface NFCPushOptions {
target?: NFCPushTarget,
timeout?: number,
ignoreRead?: boolean,
}
type NFCPushTarget =
"tag" |
"peer" |
"any";
}
export {}
@richardszalay
Copy link
Author

@MustafaUzumcu That detection code looks fine. Are you accessing the site via HTTPS? That's a restriction, if I recall correctly.

Btw if you get passed the detection, it should be message.records not message.data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment