Defining a NDEF Message

Question:

I am working on a Python project with my Raspberry Pi and the RFID-RC522 board. As NFC tags I use NXP NTAG213. My plan now is to store links on the tags. I can read/write on them without a problem. But I don’t understand how to define the NDEF header for the stored data on the tags.

When I write a link on the tags with my smartphone and read it with my program the stored data on the tag looks like this:

The NDEF header for the URL “http://www.gmx.at” is

[3, 11, 209, 1, 7, 85, 1, ... (Data)]

I recognized that some of these parameters change when I write another link, but some still stay the same.

I found this tutorial which describes the different fields of the NDEF header but I still don’t get how I need to set them to store a link to a website.

I would be very happy if someone can describe how I need to calculate/define the parameters for a link correctly.

Answers:

In order to understand the NDEF format and the way how NDEF formatted data is stored on an NFC Forum Type 2 tag (which is the tag platform that is implemented by the NTAG213), I suggest that you read the following NFC Forum specifications:

The data that you read from the tag is an NDEF Message TLV object containing an NDEF message that consists of one URI record.

  • NDEF Message TLV:

      0x03             TLV tag = NDEF Message TLV
        0x0B           TLV length = 11 bytes
        0xD1 ... 0x74  TLV value = NDEF message
      

    This means that the tag contains an NDEF message with a length of 11 bytes. The NDEF message is 0xD1 ... 0x74.

  • NDEF Message:

      0xD1             Record header
                         Bit 7 = MB = 1: first record of NDEF message
                         Bit 6 = ME = 1: last record of NDEF message
                         Bit 5 = CF = 0: last or only record of chain
                         Bit 4 = SR = 1: short record length field
                         Bit 3 = IL = 0: no ID/ID length fields
                         Bit 2..0 = TNF = 0x1: Type field represents an NFC Forum
                                               well-known type name
        0x01           Type length = 1 byte
        0x07           Payload length = 7 bytes
        0x55           Type field = "U" (in US-ASCII) = binary form of type name urn:nfc:wkt:U
        0x01 ... 0x74  Payload field = URI record payload
      

    This means that the NDEF message consists of one URI record (type name urn:nfc:wkt:U) following the URI Record Type Definition.

  • URI record payload:

      0x01             Identifier byte = URI prefix "http://www."
      0x67 ... 0x74    URI field (UTF-8 encoded) = "gmx.at"
      

    This means that the URI record points to the URI "http://www.gmx.at".

Answered By: Michael Roland

As you are using Python, you can also use the ndeflib package to encode the data.

For your example:

import ndef

record1 = ndef.UriRecord(f"http://www.gmx.at")
message = [record1]
buf = b"".join((ndef.message_encoder(message)))
print(buf.hex())

Output:

d101075501676d782e6174

You will need to wrap this in a TLV (Tag Length Value) structure:

03  # TLV T field: block contains NDEF message
0B  # TLV L field: NDEF message length = 11
:   # TLV V field: NDEF message data (from above)
FE  # TLV terminator

Those are the bytes that you should write to the tag’s user memory.

Answered By: jrc
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.