Android nfcv.transceive() throws an exception

大城市里の小女人 提交于 2019-12-06 16:38:37

You receive a TagLostException because your command is in a wrong format and, consequently, the tag does not answer.

The READ SINGLE BLOCK command (command code 0x20) reads, as its name suggests, a single block. Therefore, there is no length ("number of blocks") field in this command. The correct command would look like this:

int blockAddress = 0;
byte[] cmd = new byte[] {
        (byte) 0x00,  // FLAGS
        (byte) 0x20,  // READ_SINGLE_BLOCK
        (byte)(blockAddress & 0x0ff)
};
byte[] response = nfcv.transceive(cmd);

Note that if the tag does not understand the command (the READ SINGLE BLOCK is an optional command in ISO/IEC 15693), you may still get TagLostException then.

Finally, some Android platforms do not work well (or simply do not support) unaddressed commands for NFC-V. You might therefore want to use the addressed form of that command instead:

byte[] tagUid = tag.getId();  // store tag UID for use in addressed commands

int blockAddress = 0;
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);  // paste tag UID into command
byte[] response = nfcv.transceive(cmd);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!