-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (33 loc) · 1.19 KB
/
script.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
35
36
37
38
// Check for NFC support
function log(message) {
document.getElementById("log-container").textContent = message;
}
if (navigator.nfc) {
log("NFC Supported");
// Function to handle successful tag read
function handleReadSuccess(nfcEvent) {
const tag = nfcEvent.target;
const tagByteArray = nfcEvent.data.getBytes(); // Get raw byte data from tag
const content = new TextDecoder().decode(tagByteArray); // Decode bytes to string
document.getElementById("content-container").textContent = content;
}
// Function to handle reading errors
function handleReadError(error) {
log("ERROR : NFC Read Error:" + error);
// Display error message to user
}
// Request user permission to use NFC
navigator.nfc.requestPermission()
.then(() => {
// Start listening for NFC tags
navigator.nfc.onNfcDetected = handleReadSuccess;
navigator.nfc.onerror = handleReadError;
})
.catch(error => {
log("ERROR : NFC Permission Error:" + error);
// Handle permission denial
});
} else {
log("ERROR : NFC Not Supported");
// Display message informing user that NFC is not available
}