0
votes

I'm developing a nfc writer app. Mifare Ultralight tags, ACR122 reader. The reader shows me the "TAG_ISO_14443_3" standard. node.js, nfc-pcsc lib. I need to write a URL link to website, so my writing tag function looks like this (I found the NDEF structure example here link):

async function writeUrl (reader, url, block = 4) {
//   +-------------------------+----------------------------------------------------------------
//   | D1                      | Header flags 11010001 (MB = ME = 1, CF = 0, SR = 1, IL = 0, TNF = 0x1) TNF = 0x1 is "well known NFC format"
//   +-------------------------+----------------------------------------------------------------
//   | 01                      | Type Length (1 byte)
//   +-------------------------+----------------------------------------------------------------
//   | N                       | Payload Length (N bytes)
//   +-------------------------+----------------------------------------------------------------
//   | 55                      | Type Name ("U")
//   +-------------------------+----------------------------------------------------------------
//   | 04 ...                  | Payload: Identifier code = 4 (prefix "https://"),
//   |                         |          truncated URI = url
//   +-------------------------+----------------------------------------------------------------

  let flagsByte = parseInt('D1', 16)
  let typeLength = 1
  let payloadLength = 1 + url.length // identifierByte + url
  let typeName = parseInt('55', 16) // 'U', Url
  let header = Buffer.from([flagsByte, typeLength, payloadLength, typeName]) // 4 bytes
  let identifier = Buffer.from([parseInt('04', 16)]) // prefix https://, 1 byte
  let urlBuffer = Buffer.from(url, 'utf8')
  let payload = Buffer.concat([identifier, urlBuffer])
  let length = header.length + payload.length
  let remains = length % 4
  length += (4 - remains) // we can write only by blocks (4 bytes)
  if (length > 144) {
    length = 144 // maximum bytes possible
  }

  let data = Buffer.concat([header, payload], length)

  reader.write(block, data);
  console.log(`data written`, reader, data) // data has 30+ not-zero bytes, all ok
  return true;
}

What's the problem? After I wrote the message, my NFC reader app (GoToTags Windows app) shows me that my tag is empty. 'empty' tag

Please help. I think I've missed something small but important...

1

1 Answers

1
votes

There is more to NDEF than writing the data for a single NDEF record which you seem have crafted.

A NDEF Message can have multiple records.

You might want to use another library like https://www.npmjs.com/package/ndef to correctly create a full NDEF Message easily

The once you have a Full NDEF message you can then write that to a card.

I suggest your read http://apps4android.org/nfc-specifications/NFCForum-TS-Type-2-Tag_1.1.pdf as you are using an NTAG213 which is a type 2 card as this give details about how read and write NDEF messages to these cards.

A quick synopsis:-

  • Read Capability Container to find start and end block of the area that NDEF messages can be written to
  • Write the Start T L data
  • Write the NDEF message
  • Write the End T data

This hopefully should give you an understanding of the what is required to write NDEF data using low level commands. (As a note I don't do NFC with JS so cannot help in more detail)