forked from manekinekko/puzzle-duo-nfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-ndef.html
63 lines (52 loc) · 1.69 KB
/
index-ndef.html
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
<button id="scanButton">Scan</button>
<button id="writeButton">Write</button>
<audio src="/meow.wav" id="meow"></audio>
<script>
scanButton.addEventListener("click", async () => {
console.log("User clicked scan button");
try {
const ndef = new NDEFReader();
await ndef.scan();
console.log("> Scan started");
ndef.addEventListener("readingerror", () => {
console.log("Argh! Cannot read data from the NFC tag. Try another one?");
});
ndef.addEventListener("reading", ({ message, serialNumber }) => {
console.log(`> Serial Number: ${serialNumber}`);
console.log(`> Records: (${message.records.length})`);
meow.play();
/*
if (message.records.length === 0) return ''
return message.records
.map((r) => {
const record = r as NDEFRecord
switch (record.recordType) {
case 'text':
const textDecoder = new TextDecoder(record.encoding)
if (!record.data) return ''
return textDecoder.decode(record.data)
case 'url':
const decoder = new TextDecoder()
return decoder.decode(record.data)
default:
return ''
}
})
.join(', ')
*/
});
} catch (error) {
console.log("Argh! " + error);
}
});
writeButton.addEventListener("click", async () => {
console.log("User clicked write button");
try {
const ndef = new NDEFReader();
await ndef.write("Hello world!");
console.log("> Message written");
} catch (error) {
console.log("Argh! " + error);
}
});
</script>