I am trying to create and write an NDEF message to an NFC tag in a Windows Form Application (written in C#) using an ACR122U NFC reader.
I have created raw bytes of the NDEF message using Andreas Jakl's NDEF library. This is the C# code:
var spRecord = new NdefTextRecord {
Text = "1",
LanguageCode = "en"
};
var msg = new NdefMessage { spRecord };
string hex = BitConverter.ToString(msg.ToByteArray());
resultBox.Text = hex.Replace('-',' ');
The output I get is D1 01 04 54 02 65 6E 31 (hexadecimal).
Then I'm writing this data to an NFC tag (MIFARE Ultralight) starting at block #5 using the following APDU commands:
CL INS P1 P2 Lc DATA IN
FF D6 00 05 04 D1 01 04 54
CL INS P1 P2 Lc DATA IN
FF D6 00 05 04 02 65 6E 31
But when I try to read that tag using Android, the written NDEF message is not recognized.
What do I need to do in order to get the NDEF message recognized by Android?
Solution (Thanks Michael Roland)
I wrote an NDEF tag using an Android app and then compared the values I have generated on that tag with the tag I wrote using the above method. The difference was 0x03 0x08 at the start. So 0x03 is a starting byte that is required and 0x08 is the length of the NDEF message.
FF D6 00 04 04 03 08 D1 01
FF D6 00 05 04 04 54 02 65
FF D6 00 06 04 6E 31 FE 00
You can't just write the NDEF data at random locations inside the tag and then expect the data to be discoverable by other devices. By the way, note that both of your write commands seem to write the different data blocks to the same block number on the tag.
MIFARE Ultralight tags match the NFC Forum Type 2 Tag Operation specification. Thus, you need to implement the Type 2 Tag Operation specification to properly write data to this type of NFC tag.
Therefore, you would need to first make sure that the tag contains a properly configured capability container on block 3. For MIFARE Ultralight, this could be something like E1 10 06 00
. (Note that a different CC may be necessary for other tag types like Ultralight C and various NTAG tags. Also note that you can only set bits in the CC block but you cannot clear them once set, thus be careful with the value you write in there.)
Next, you can wrap the NDEF message that you get as output from the NDEF library into an NDEF message TLV (tag-length-value) structure. The tag is 0x03
, followed by one length byte, followed by the actual NDEF data. Hence, for the above NDEF message, this would look like 03 08 D1 01 04 54 02 65 6E 31
. You would then add a terminator TLV (0xFE
) to that data blob and fill with zeros to align to a multiple of the block size:
03 08 D1 01
04 54 02 65
6E 31 FE 00
Now you can write those three blocks to the tag starting at block 4. E.g.,
FF D6 00 04 04 03 08 D1 01
FF D6 00 05 04 04 54 02 65
FF D6 00 06 04 6E 31 FE 00