-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (29 loc) · 1.03 KB
/
app.js
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
console.log('app.js');
document.addEventListener('DOMContentLoaded', () => {
var btn_read = document.getElementById('btn_read');
var div_msg = document.getElementById('message');
btn_read.addEventListener('click', async () => {
div_msg.innerText = 'clicked read button.';
// NFC読み込みイベント登録
try {
const ndef = new NDEFReader();
await ndef.scan();
div_msg.innerText = 'scan started';
ndef.addEventListener('error', e => {
div_msg.innerText = 'error:' + e;
});
ndef.addEventListener('reading', e => {
div_msg.innerText = `message=${e.message}, serialNumber=${e.serialNumber}`;
for(const record of e.message.records){
if(record.recordType === 'text'){
const decorder = new TextDecoder(record.encoding);
const data = decoder.decode(record.data);
div_msg.innerText = data;
}
}
});
} catch (error) {
div_msg.innerText = 'try:error:' + error;
}
});
});