Android NFC 堆栈自动为您处理轮询(搜索各种标签技术/协议的标签)、防冲突(枚举一种标签技术/协议中的多个标签)和激活(启动与一个特定标签的通信)。因此,您绝对不应该自己发送用于防冲突和激活的命令。 Inventory 命令就是这样一种命令(用于发现范围内的标签)。
对于 Inventory 命令,通常不需要发送该命令。 Android NFC API 已提供您从此命令获得的所有信息:
- 您可以使用以下方式获取 UID
tag.getId()
.
- 您可以使用以下方式获取 DSFID
tech.getDsfId()
.
此外,为了使您的应用程序能够在不同的 Android 设备平台(= 不同的 NFC 堆栈)上可靠地工作,您应该始终使用命令的寻址版本(即作为请求的一部分发送的 Address_flag 设置和 UID)。看Android NfcV 获取信息命令仅返回一个字节 https://stackoverflow.com/q/36951077/2425802.
如果要读取/写入标签,可以使用 READ_SINGLE_BLOCK 和 WRITE_SINGLE_BLOCK 命令:
byte[] tagUid = tag.getId(); // store tag UID for use in addressed commands
int blockAddress = 0; // block address that you want to read from/write to
READ_SINGLE_BLOCK:
byte[] cmd = new byte[] {
(byte)0x20, // FLAGS
(byte)0x20, // READ_SINGLE_BLOCK
0, 0, 0, 0, 0, 0, 0, 0,
(byte)(blockAddress & 0x0ff)
};
System.arraycopy(tagUid, 0, cmd, 2, 8);
byte[] response = tech.transceive(cmd);
WRITE_SINGLE_BLOCK:
byte[] cmd = new byte[] {
(byte)0x60, // FLAGS
(byte)0x21, // WRITE_SINGLE_BLOCK
0, 0, 0, 0, 0, 0, 0, 0,
(byte)(blockAddress & 0x0ff),
... // data block that you want to write (same length as the blocks that you read)
};
System.arraycopy(tagUid, 0, cmd, 2, 8);
byte[] response = tech.transceive(cmd);