libnfc:从 NTAG 写入和读回自定义数据

libnfc: Write and read back custom data from NTAG

我已经尝试了很长时间从我的 NFC 卡中写入和读取数据。这些卡是 NTAG216。我可以使用 libnfc 示例来读取制造商 ID,它工作正常。但我需要向每个标签写入一些自定义数据,例如字符串 "abcdefg" 并读回。

我已经阅读了几乎所有我能找到的关于 libnfc 的相关内容,但所有这些都没有讨论这个特定领域。我将不胜感激任何指导。代码示例将不胜感激。

我使用 ACRU122 NFC reader/writer 和 libnfc 1.7.1 from Github

枚举并激活标签后,您可以使用方法 nfc_initiator_transceive_bytes() to send raw NTAG commands (such as READ/WRITE) to the tag. Since the command set of NTAG216 is compatible to the MIFARE Ultralight command set (including the COMPATIBILITY WRITE command), you could also use the convenience methods for MIFARE tags defined in utils/mifare.h:

  • nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp)
  • nfc_initiator_mifare_cmd(pnd, MC_READ, page, &mp)

有关完整的代码示例,请参阅 nfc-mfultralight 实用程序:nfc-mfultralight.c

但是,您可能希望修改该代码以使用更高效的 MIFARE Ultralight/NTAG WRITE (0xA2) 命令而不是 COMPATIBILITY WRITE(MC_WRITE 使用的命令)。