我使用以下代码在 NTAG213 NFC 标签上设置 AUTH0(需要密码验证的第一页):
try {
result = nfca.transceive(new byte[]{
(byte) 0xA2, // Command: WRITE
(byte) 0x29, // Address: AUTH0
(byte) 0x00 // starting address
});
} catch (IOException e) {
e.printStackTrace();
}
但是,当我在 AUTH0 上写入 00h(作为起始地址)时,我总是得到异常:“Transceive failed”。
你能告诉我这里可能出了什么问题吗?
最佳答案
NTAG213(与其他 NTAG 和 MIFARE Ultralight 芯片一样)使用 4 字节的页面大小。 WRITE 命令 (0xA2) 只能用于写入整页。因此,WRITE 命令的数据参数需要由 4 个字节组成。
最简单的方法是覆盖整个配置页面:
result = nfca.transceive(new byte[]{
(byte) 0xA2, // Command: WRITE
(byte) 0x29, // Address: CONFIG0
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
});
但请记住,这也会覆盖其他配置参数(镜像字节和镜像页面)。如果你想将其他参数设置为默认值,你可以简单地使用这个:
result = nfca.transceive(new byte[]{
(byte) 0xA2, // Command: WRITE
(byte) 0x29, // Address: CONFIG0
(byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00
});
但是,如果您希望将其他值保留为当前包含的值,您可能需要先读取页面,然后使用这些值更新页面(仅将 AUTH0 设置为 0x00):
byte[] currentData = nfca.transceive(new byte[]{
(byte) 0x30, // Command: READ
(byte) 0x29, // Address: CONFIG0
});
result = nfca.transceive(new byte[]{
(byte) 0xA2, // Command: WRITE
(byte) 0x29, // Address: CONFIG0
currentData[0], currentData[1], currentData[2], (byte) 0x00
});
关于android - 在 NTAG213 中获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34543076/