1

I am trying to read the NFC tag using Web NFC. I've received the message object, I am able to read the record message.records[0], but I am not able to get the plain text "Hello world" stored in that NFC record.

scanRfid.addEventListener("click", async () => {
  //curlog("User clicked scan button");

  try {
    const reader = new NDEFReader();
    await reader.scan();
   // curlog("> Scan started");

    reader.addEventListener("error", (event) => {
      console.log(`Argh! ${event.message}`);
    });

    reader.addEventListener("reading", ({ message, serialNumber }) => {
      
      //curlog(`> Records: (${message.records})`);
      //processMessage(message);
      console.log(message.records[0[);
      
    });
  } catch (error) {
    curlog("Argh! " + error);
  }
});
Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
Maciej K
  • 11
  • 4
  • I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your question. In general, you should [edit] the question to *clarify* it, but not to include an answer within it. You should create your own answer with the code/solution you used to solve your problem, and then accept it (the system may require a 48 hour delay prior to doing so). When you've solved the problem yourself, [answering your own question is encouraged](/help/self-answer). – double-beep Oct 03 '20 at 10:18

1 Answers1

0

As described at https://web.dev/nfc/#read-and-write-a-text-record, the text record data can be decoded with a TextDecoder instantiated with the record encoding property. Note that the language of the text record is available through its lang property.

function readTextRecord(record) {
  console.assert(record.recordType === "text");
  const textDecoder = new TextDecoder(record.encoding);
  console.log(`Text: ${textDecoder.decode(record.data)} (${record.lang})`);
}
François Beaufort
  • 4,843
  • 3
  • 29
  • 38