Commit b9ddcd24 authored by SerHack's avatar SerHack
Browse files

Update script.js

parent 33f71c66
// Script for reading, writing NFC tags.
/* if (!('NDEFReader' in window)) {
console.log("NFC reading not supported");
alert('NFC not enabled');
} */
// part for writing
async function readTag() {
if ("NDEFReader" in window) {
const reader = new NDEFReader();
try {
await reader.scan();
reader.onreading = event => {
const decoder = new TextDecoder();
for (const record of event.message.records) {
consoleLog("Record type: " + record.recordType);
consoleLog("MIME type: " + record.mediaType);
consoleLog("=== data ===\n" + decoder.decode(record.data));
}
}
} catch(error) {
consoleLog(error);
}
} else {
consoleLog("Web NFC is not supported.");
}
}
function writeNFC(message){
const writer = new NDEFWriter();
writer.write(message).then(() => {
alert("Message written.");
}).catch(error => {
alert(`Write failed :( try again: ${error}.`);
});
async function writeTag() {
if ("NDEFWriter" in window) {
const writer = new NDEFWriter();
try {
await writer.write("What Web Can Do Today");
consoleLog("NDEF message written!");
} catch(error) {
consoleLog(error);
}
} else {
consoleLog("Web NFC is not supported.");
}
}
var message = "hello world";
writeNFC(message);
\ No newline at end of file
function consoleLog(data) {
var logElement = document.getElementById('log');
logElement.innerHTML += data + '\n';
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment