Encoding a URI in an NFC NDEF message
David CoxThe whole point of Near Field Communication (NFC) is the NDEF message (or NFC Data Exchange Format message). That’s where the goods are stored. You can’t just type in any old text though. You have to use the right format, and you have to encode it in hexadecimal. It’s not the most fun thing in the world, but it is doable. I’ve written a series of blog posts to help you do it.
Encoding the Data
An NDEF message is made up of multiple chunks of data called TLVs. The TLVs provide data to the NFC reader, and let the reader know what to do with it. The most basic (and most common) NDEF message will include an NDEF record TLV and a terminal TLV.
I’ve written a blog post on encoding a URI into an NDEF record TLV. This is the resulting TLV:
03 0E D1 01 0A 55 01 77 69 73 6B 64 2E 63 6F 6D
One TLV down. One to go.
Finishing it Off
Once you have your NDEF record TLV, you’re practically done with the entire NDEF message. All you need now is the terminal TLV. If you didn’t catch it before, that same blog post has a section on creating a terminal TLV. This is the resulting terminal TLV:
FE
That’s all the TLVs you need.
Putting it Together
With your NDEF record TLV and your terminal TLV, your NDEF message is essentially done. Just start the message off with your NDEF record TLV and stick the terminal TLV on the end. This is what it looks like:
03 0E D1 01 0A 55 01 77 69 73 6B 64 2E 63 6F 6D FE
Yeah, that’s the NDEF message. If you want to encode that message to an actual NFC tag, I’ll be writing a post about that soon.