写入单块命令会因NfcV失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写入单块命令会因NfcV失败相关的知识,希望对你有一定的参考价值。

我正在尝试通过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;
        }
    }
}

答案

将标志更改为(字节)0x22值可修复问题。

以上是关于写入单块命令会因NfcV失败的主要内容,如果未能解决你的问题,请参考以下文章

NFC 标签类型 NfcV Android Studio 的 SetTimeout()

当我从 TabFragment 使用 LoaderManager 启动片段时,为啥 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃?

为啥 sed 会因国际字符而失败以及如何解决?

为啥标量值函数会因空参数而失败?

套接字:为啥阻塞 read() 会因 ENOTCONN 而失败?

为啥 TextOutW() 会因自定义字体大于 131 像素而失败?