Android nfcv.transceive()引发异常 [英] Android nfcv.transceive() throws an exception

查看:249
本文介绍了Android nfcv.transceive()引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Android应用,该应用使用transceive()功能与NFC-V卡进行通信.我的问题是那条线

I wrote an Android app that uses the transceive() function to communicate with an NFC-V card. My problem is that line

byte[] response = nfcv.transceive(command)

总是抛出标记丢失异常.

always throws a tag lost exception.

有人可以帮我吗?

String action = intent.getAction();

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV nfcv = NfcV.get(tag);
if(nfcv != null) {
    Toast.makeText(this, "nfcv detected", Toast.LENGTH_LONG).show();
}

try {
    nfcv.connect();
    Toast.makeText(this, "connected", Toast.LENGTH_LONG).show();
    byte[] command = new byte[]{                      
            (byte) 0x00, // Flags
            (byte) 0x20, // Command: Read single block
            (byte) 0x00, // First block (offset)
            (byte) 0x04  // Number of blocks};
    byte[] response = nfcv.transceive(command);
    nfcv.close();
} catch(Exception e) {
    Toast.makeText(this, "Error exception!", Toast.LENGTH_LONG).show();
}

我收到以下异常:

android.nfc.TagLostException: Tag was lost.
    at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:48)
    at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151)
    at android.nfc.tech.NfcV.transceive(NfcV.java:115)
    at com.example.nxf07589.nfc.MainActivity.onCreate(MainActivity.java:148)
    at android.app.Activity.performCreate(Activity.java:6374)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
    at android.app.ActivityThread.access$900(ActivityThread.java:182)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6141)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

推荐答案

您收到的TagLostException是因为您的命令格式错误,因此该标记未响应.

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

READ SINGLE BLOCK命令(命令代码0x20)顾名思义就是读取单个块.因此,此命令中没有长度(块数")字段.正确的命令应如下所示:

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);

请注意,如果标签不理解该命令("READ SINGLE BLOCK"是ISO/IEC 15693中的可选命令),则您仍可能会得到TagLostException.

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.

最后,某些Android平台在NFC-V上无法很好地运行(或根本不支持)未寻址的命令.因此,您可能希望改用该命令的寻址形式:

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);

这篇关于Android nfcv.transceive()引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆