【发布时间】:2016-08-04 13:10:33
【问题描述】:
我编写了一个使用transceive() 函数与NFC-V 卡通信的Android 应用。我的问题是那条线
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)
【问题讨论】:
-
您正在丢弃异常消息和堆栈跟踪,这对于此类问题非常有用。在 catch 块中的 toast 下方,请添加
android.util.Log.e("NFC", "Exception", e);以读取 LogCat 中的异常信息。然后将堆栈跟踪添加到您的帖子中。
标签: android nfc ioexception iso-15693