gpt4 book ai didi

Android nfcv.transceive() 抛出异常

转载 作者:行者123 更新时间:2023-11-30 01:07:15 32 4
gpt4 key购买 nike

我编写了一个 Android 应用程序,它使用 transceive() 函数与 NFC-V 卡进行通信。我的问题是那条线

byte[] response = nfcv.transceive(command)

总是抛出标签丢失异常。

有人可以帮我吗?

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,因为您的命令格式错误,因此标签没有响应。

正如其名称所示,READ SINGLE BLOCK 命令(命令代码 0x20)读取单个 block 。因此,此命令中没有长度(“ block 数”)字段。正确的命令应该是这样的:

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

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

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() 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38768310/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com