Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js #775

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions web-nfc/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
scanButton.addEventListener("click", async () => {
scanButton.addEventListener("click", async () => {
log("User clicked scan button");

try {
const ndef = new NDEFReader();
await ndef.scan();
log("> Scan started");

ndef.addEventListener("readingerror", () => {
log("Argh! Cannot read data from the NFC tag. Try another one?");
});

ndef.addEventListener("reading", ({ message, serialNumber }) => {
log(`> Serial Number: ${serialNumber}`);
log(`> Records: (${message.records.length})`);
});
} catch (error) {
log(`> Records: (${message.records.length})`);
for (const record of message.records) {
log(`\nRecord id: ${record.id}`);
log(`Record type: ${record.recordType}`);
log(`MIME type: ${record.mediaType}`);
switch (record.recordType) {
case "text":
{
const textDecoder = new TextDecoder(record.encoding);
log(`Text: ${textDecoder.decode(record.data)} (${record.lang})`);
}
break;
case "url":
{
const textDecoder = new TextDecoder();
log(`URL: ${textDecoder.decode(record.data)}`);
}
break;
default:
// TODO: Handle other records with record data.
}
}
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we have a spacer to make it clear there are multiple records?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. I want to further parse other types of tags as well. is this fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'd parse more types in a sample like this.

} catch (error) {
log("Argh! " + error);
}
});
Expand Down