Commit 70e1298f authored by SerHack's avatar SerHack
Browse files

Update script.js

parent b9ddcd24
// function called when the "Test read NFC" link is pushed
async function readTag() {
// if NFC is enabled
if ("NDEFReader" in window) {
const reader = new NDEFReader();
try {
// NFC works and it keeps scanning
await reader.scan();
// NFC found a tag
reader.onreading = event => {
// Decoding the text from a NFC TAG requires TextDecoder
const decoder = new TextDecoder();
// event.message.records contains Record, MIME Type and the content
for (const record of event.message.records) {
consoleLog("Record type: " + record.recordType);
consoleLog("MIME type: " + record.mediaType);
......@@ -12,6 +18,7 @@ async function readTag() {
}
}
} catch(error) {
// Ohh something happened
consoleLog(error);
}
} else {
......@@ -19,13 +26,19 @@ async function readTag() {
}
}
// function called when the "Test write NFC" link is pushed
async function writeTag() {
// if NFC is enabled
if ("NDEFWriter" in window) {
const writer = new NDEFWriter();
// try to write a message
try {
// NFC API tries to write to NFC tag the message "What Web Can Do Today" and it waits an answer
await writer.write("What Web Can Do Today");
// yay!
consoleLog("NDEF message written!");
} catch(error) {
// Something bad just happen
consoleLog(error);
}
} else {
......@@ -34,6 +47,7 @@ async function writeTag() {
}
function consoleLog(data) {
// this function was written to simplify the writing "logs" into a HTML element. :)
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