简体   繁体   English

使用ACR122U作为读写器在Windows Form Application C#中将Ndef写入NFC标签

[英]Writing Ndef to NFC tag in Windows Form Application C# using ACR122U as Reader/Writer

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. 我正在尝试使用ACR122U NFC阅读器在Windows窗体应用程序(用C#编写)中创建NDF消息并将其写入NFC标签。

I have created raw bytes of the NDEF message using Andreas Jakl's NDEF library . 我已经使用Andreas Jakl的NDEF库创建了NDEF消息的原始字节。 This is the C# code: 这是C#代码:

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). 我得到的输出是D1 01 04 54 02 65 6E 31 (十六进制)。

Then I'm writing this data to an NFC tag (MIFARE Ultralight) starting at block #5 using the following APDU commands: 然后,我使用以下APDU命令将此数据写入第5块开始的NFC标签(MIFARE Ultralight):

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. 但是,当我尝试使用Android读取该标签时,无法识别书面的NDEF消息。

What do I need to do in order to get the NDEF message recognized by Android? 为了使Android识别NDEF消息,我需要做什么?

Solution (Thanks Michael Roland) 解决方案(感谢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. 我使用Android应用程序编写了NDEF标签,然后将在该标签上生成的值与使用上述方法编写的标签进行了比较。 The difference was 0x03 0x08 at the start. 开始时的差异为0x03 0x08。 So 0x03 is a starting byte that is required and 0x08 is the length of the NDEF message. 因此,0x03是必需的起始字节,而0x08是NDEF消息的长度。

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. 您不仅可以将NDEF数据写入标签内的随机位置,然后期望其他设备可以发现该数据。 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. MIFARE Ultralight标签符合NFC论坛2类标签操作规范。 Thus, you need to implement the Type 2 Tag Operation specification to properly write data to this type of NFC tag. 因此,您需要实现2型标签操作规范 ,才能将数据正确写入此类型的NFC标签。

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 . 因此,您首先需要确保标签在块3上包含正确配置的功能容器。对于MIFARE Ultralight,这可能类似于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.) (请注意,对于其他标签类型(例如Ultralight C和各种NTAG标签),可能需要使用不同的CC。还要注意,您只能在CC块中设置位,但是一旦设置就不能清除它们,因此请小心输入值那里。)

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. 接下来,您可以将从NDEF库输出的NDEF消息包装到NDEF消息TLV(标记长度值)结构中。 The tag is 0x03 , followed by one length byte, followed by the actual NDEF data. 标签为0x03 ,后跟一个长度字节,后跟实际的NDEF数据。 Hence, for the above NDEF message, this would look like 03 08 D1 01 04 54 02 65 6E 31 . 因此,对于上面的NDEF消息,这看起来像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: 然后,您需要向该数据blob添加一个终结器TLV( 0xFE ),并用零填充以与块大小的倍数对齐:

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. Eg, 现在,您可以将这三个块写到从块4开始的标签中。例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM