使用ACR122U作为读写器在Windows Form Application C#中将Ndef写入NFC标签 [英] Writing Ndef to NFC tag in Windows Form Application C# using ACR122U as Reader/Writer

查看:646
本文介绍了使用ACR122U作为读写器在Windows Form Application C#中将Ndef写入NFC标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ACR122U NFC阅读器在Windows窗体应用程序(用C#编写)中的NFC标签上创建NDEF消息并将其写入.

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.

我已经使用Andreas Jakl的 NDEF库创建了NDEF消息的原始字节.这是C#代码:

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('-',' ');

我得到的输出是 D1 01 04 54 02 65 6E 31 (十六进制).

The output I get is D1 01 04 54 02 65 6E 31 (hexadecimal).

然后,我使用以下APDU命令从第5块开始将此数据写入NFC标签(MIFARE Ultralight):

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

但是当我尝试使用Android读取该标签时,无法识别书面的NDEF消息.

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?

解决方案(感谢Michael Roland)

Solution (Thanks Michael Roland)

我使用Android应用程序编写了NDEF标签,然后将在该标签上生成的值与使用上述方法编写的标签进行了比较.开始时的差异为0x03 0x08.因此,0x03是必需的起始字节,而0x08是NDEF消息的长度.

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

推荐答案

您不能只是将NDEF数据写入标签内的随机位置,然后期望其他设备可以发现该数据.顺便说一下,请注意,您的两个写入命令似乎都将不同的数据块写入标签上的相同块号.

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标签符合NFC论坛2类标签操作规范.因此,您需要实现类型2标签操作规范将数据正确写入此类型的NFC标签.

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.

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

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.)

接下来,您可以将从NDEF库输出的NDEF消息包装到NDEF消息TLV(标记长度值)结构中.标签为0x03,后跟一个长度字节,后跟实际的NDEF数据.因此,对于上面的NDEF消息,这看起来像03 08 D1 01 04 54 02 65 6E 31.然后,您需要在该数据blob中添加一个终结器TLV(0xFE),并用零填充以与块大小的倍数对齐:

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

现在您可以将这三个块写到从第4块开始的标签中.例如,

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

这篇关于使用ACR122U作为读写器在Windows Form Application C#中将Ndef写入NFC标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆