编写单块命令对NfcV进行故障转移

时间:2019-04-25 19:48:29

标签: android tags nfc rfid iso-15693

我正在尝试通过NfcV对象使用WRITE SINGLE BLOCK(0x21)命令将数据写入NXP I-CODE SLI(ISO 15693-型号:IQC21-50P)。

以下代码成功读取了标签:

Tag currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = currentTag.getId();
for (String tech : currentTag.getTechList()) {
    if (tech.equals(NfcV.class.getName())) {
        NfcV nfcvTag = NfcV.get(currentTag);
        try {
            nfcvTag.connect();
            // nfcvTag.getMaxTransceiveLength() returns 253
            int offset = 0;  // offset of first block to read
            int blocks = 8;  // number of blocks to read
            byte[] cmd = new byte[] {
                    (byte) 0x60,  // flags: addressed (= UID field present)
                    (byte) 0x23, // command: READ MULTIPLE BLOCKS
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,  // placeholder for tag UID
                    (byte) (offset & 0x0ff),  // first block number
                    (byte) ((blocks - 1) & 0x0ff)  // number of blocks (-1 as 0x00 means one block)
            };
            System.arraycopy(id, 0, cmd, 2, 8);
            byte[] response = nfcvTag.transceive(cmd);
        } 
        catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
    }
}

当我尝试使用以下代码将数据写入标签时,数据也不会写入标签,并且也没有出现任何错误。

这是我使用的写单个块代码:

Tag currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = currentTag.getId();
String dataString = "CGH13F1V0AK1234567:6 012 ";

for (String tech : currentTag.getTechList()) {
    if (tech.equals(NfcV.class.getName())) {
        NfcV nfcvTag = NfcV.get(currentTag);
        try {
            nfcvTag.connect();
            int offset = 0;  // offset of first block to read
            int blocks = 8;  // number of blocks to read
            byte[] data = convertHexToByte(convertStringToHex(dataString));
            byte[] cmd = new byte[] {
                    (byte)0x60, // FLAGS
                    (byte)0x21, // WRITE SINGLE COMMAND
                    (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, // UID
                    (byte)0x00, // OFFSET
                    (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00  //DATA
            };
            System.arraycopy(id, 0, cmd, 2, 8);

            for (int i = 0; i < blocks; ++i) {
                cmd[10] = (byte)((offset + i) & 0x0ff);
                System.arraycopy(data, 4 * i, cmd, 11, 4);

                byte[] response = nfcvTag.transceive(cmd);
            }

        } 
        catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将标志更改为(字节)0x22值可以解决此问题。