如何使用与Android兼容的C#读写NDEF格式的NFC标签? [英] How to read and write to NFC tags in NDEF format using C# compatible with Android?

查看:409
本文介绍了如何使用与Android兼容的C#读写NDEF格式的NFC标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mifare Ultralight C 标签并将数据写入NDEF格式化给他们.我正在使用 NDEF-NFC库为我的NDEF消息创建原始字节.我的NDEF消息是

I am working with Mifare Ultralight C tags and write data in NDEF format to them. Am using NDEF-NFC library to create raw bytes for my NDEF message. My NDEF message is

var spRecord = new NdefTextRecord {
        Text = "1",
        LanguageCode = "en"
};
var msg = new NdefMessage { spRecord };

我得到的输出是D1 01 04 54 02 65 6E 31(十六进制).如果我按原样将此字节数组写入标记:

The output I get is D1 01 04 54 02 65 6E 31 (hexadecimal). If I write this byte array to the tag as-is:

  • 从标签读取并使用相同的NDEF-NFC库后,我可以转换回所需的NDEF消息.
  • Android应用程序无法识别NDEF消息.我尝试了Android版 NFC工具.

按照 在Windows窗体应用程序C#中使用ACR122U作为读取器/写入器将Ndef写入NFC标签的建议 ,我修改了字节数组以写入03 08 D1 01 04 54 02 65 6E 31 FE 00,而不是库生成的原始字节数组.

Following the suggestions in Writing Ndef to NFC tag in Windows Form Application C# using ACR122U as Reader/Writer, I modified my byte array to write 03 08 D1 01 04 54 02 65 6E 31 FE 00 instead of the original byte array produced by the library.

  • 现在我可以使用Android应用程序读取NDEF消息.
  • 但是在尝试使用库将字节数组转换为NDEF消息对象时,出现以下错误:

  • Now I'm able to read the NDEF message using Android apps.
  • But on trying to convert the byte array to an NDEF message object using the library, I get the following error:

Ndef解析错误:预期消息开始在第一条记录中丢失.

Ndef parse error: Expected Message Begin missing in first record.

如何正确读回NDEF消息?

How can I read back the NDEF message correctly?

推荐答案

您已经发现,NFC论坛2类标签(例如MIFARE Ultralight,NTAG等)要求将NDEF消息嵌入到NDEF TLV中(标签长度值)结构.这意味着您将标记03和NDEF消息的长度放在消息(值)本身的前面.因此,您得到

As you already found, NFC Forum Type 2 tags (such as MIFARE Ultralight, NTAG, etc.) require an NDEF message to be embedded into a NDEF TLV (tag-length-value) structure. This means that you prepend the tag 03 and the length of the NDEF message to the message (value) itself. Thus, you get

+-----+--------+-------------------------+
| TAG | LENGTH | VALUE                   |
| 03  | 08     | D1 01 04 54 02 65 6E 31 |
+-----+--------+-------------------------+

此外,您可以添加终结器TLV(标签= FE,长度= 00),以指示可以跳过标签上的剩余数据区域.

In addition you may add a Terminator TLV (tag = FE, length = 00) to indicate that the remaining data area on the tag can be skipped from processing.

您使用的NDEF库仅处理 NDEF消息,而不处理将数据存储在NFC标签上所需的容器格式.因此,您需要自己处理该部分.

The NDEF library that you use only processes NDEF messages and not the container format that is needed for storing the data on an NFC tag. Thus, you need to process that part yourself.

var msg = new NdefMessage { ... };
var msgBytes = msg.toByteArray();
var ndefTlvLen = new byte[(msgBytes.Length < 255) ? 1 : 3];
if (msgBytes.Length < 255) {
    ndefTlvLen[0] = (byte)(msgBytes.Length);
} else {
    ndefTlvLen[0] = (byte)0x0FF;
    ndefTlvLen[1] = (byte)((msgBytes.Length >> 8) & 0x0FF);
    ndefTlvLen[2] = (byte)(msgBytes.Length & 0x0FF);
}
var tagData = new byte[1 + ndefTlvLen.Length + msgBytes.Length + 2];
int offset = 0;
tagData[offset++] = (byte)0x003;
Array.Copy(ndefTlvLen, 0, tagData, offset, ndefTlvLen.Length);
offset += ndefTlvLen.Length;
Array.Copy(msgBytes, 0, tagData, offset, msgBytes.Length);
offset += msgBytes.Length;
tagData[offset++] = (byte)0x0FE;
tagData[offset++] = (byte)0x000;

从TLV结构中解压缩NDEF消息

var tagData = ...; // byte[]
var msg;
int offset = 0;
while (offset < tagData.Length) {
    byte tag = tagData[offset++];
    int len = (tagData[offset++] & 0x0FF);
    if (len == 255) {
        len = ((tagData[offset++] & 0x0FF) << 8);
        len |= (tagData[offset++] & 0x0FF);
    }
    if (tag == (byte)0x03) {
        var msgBytes = new byte[len];
        Array.Copy(tagData, offset, msgBytes, 0, len);
        msg = NdefMessage.FromByteArray(msgBytes);
    } else if (tag == (byte)0xFE) {
        break;
    }
    offset += len;
}

这篇关于如何使用与Android兼容的C#读写NDEF格式的NFC标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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