1

我是 Xamarin 以及 Android 开发的新手。我有一个 NFC 标签,特别是 ST M24LR64E,上面有数据。我可以使用 Google Play 上的 ST 应用程序查看数据块。在我的 Xamarin 应用程序中,我无法在没有收到 TagLostException 的情况下向标签发送消息。我可以毫无问题地查询标签 ID,但尝试读取单个数据块时,我得到了异常。任何方向将不胜感激。

byte[] response = new byte[] { 0x0A };

byte[] cmd = new byte[]
{
    (byte) 0x26,
    (byte) 0x01,
    0x00
};
response = nfcv.Transceive(cmd);

byte[] single = new byte[]
{
    (byte) 0x40, // FLAGS
    (byte) 0x20, // READ_SINGLE_BLOCK
    0, 0, 0, 0, 0, 0, 0, 0,
    (byte) (0 & 0x0ff)
};
Array.Copy(id, 0, single, 2, 8);
response = nfcv.Transceive(single);

第一个Transceive()没问题,我看到 10 个字节回来了。一旦我尝试读取数据块,就会得到 TagLostException。

4

1 回答 1

1

使用 NfcV 标签技术,aTagLostException可能表示阅读器无法再与标签通信或命令导致错误。

根据其手册,M24LR64E 仅支持 READ_SINGLE_BLOCK 命令的扩展版本(协议扩展标志集):

Protocol_extension_flag 应设置为 1 以使 M24LR64E-R 正常运行。如果 Protocol_extension_flag 为 0,则 M24LR64E-R 以错误代码回答。

因此,您的 READ_SINGLE_BLOCK 命令版本与标记不兼容。您需要设置协议扩展标志并提供 16 位块号。应该工作的版本是:

int blockNumber = 0;
byte[] readSingleBlock = new byte[] {
        (byte) 0x28, // Flags: Addressed (bit 5), Protocol Extension (bit 3)
        (byte) 0x20, // Command: READ_SINGLE_BLOCK
        0, 0, 0, 0, 0, 0, 0, 0,  // placeholder for UID
        (byte) (blockNumber & 0x0ff),
        (byte) ((blockNumber >> 8) & 0x0ff)
};
byte[] id = nfcv.GetTag().GetId();
Array.Copy(id, 0, readSingleBlock, 2, 8);
response = nfcv.Transceive(readSingleBlock);

由于您在 INVENTORY 命令中使用了高数据速率(Data_rate 标志集),您可能还希望在 READ_SINGLE_BLOCK 命令中使用高数据速率。在这种情况下,您将使用标志值0x2A(而不是0x28)。

最后,您应该避免在任何 NfcX 标签技术对象上发送防冲突/枚举命令,例如 INVENTORY 命令。虽然这可能有效,但您可能会混淆 ANdroid NFC 堆栈的内部状态保持,因为它已经为您执行这些命令并跟踪枚举的标签。您可以从Tag对象和NfcV对象获取通过 INVENTORY 请求获得的所有信息:

  • tag.GetId()为您提供标签的 UID。
  • nfcv.GetDsfId()为您提供标签的 DSFID。
  • nfcv.GetResponseFlags()为您提供 INVENTORY 响应的标志字节。
于 2016-08-30T06:18:08.123 回答