[英]Getting IOException when scanning an NFC tag weirdly
當我正常掃描NFC標簽時,我的代碼可以正常工作。 但是當我奇怪地掃描它時,例如通過滑動或其他方式,我可以在控制台中得到此錯誤:
W/System.err( 8533): java.io.IOException
W/System.err( 8533): at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:85)
W/System.err( 8533): at android.nfc.tech.Ndef.connect(Ndef.java:71)
W/System.err( 8533): at myapp.myapp.MainActivity.onNewIntent(MainActivity.java:171)
W/System.err( 8533): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1213)
W/System.err( 8533): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1225)
W/System.err( 8533): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2610)
W/System.err( 8533): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2622)
W/System.err( 8533): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2631)
W/System.err( 8533): at android.app.ActivityThread.access$1700(ActivityThread.java:169)
W/System.err( 8533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
W/System.err( 8533): at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err( 8533): at android.os.Looper.loop(Looper.java:148)
W/System.err( 8533): at android.app.ActivityThread.main(ActivityThread.java:5554)
W/System.err( 8533): at java.lang.reflect.Method.invoke(Native Method)
W/System.err( 8533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
W/System.err( 8533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
這是我的NFC代碼:
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
if (ndefMessage != null) {
String message = new String(ndefMessage.getRecords()[0].getPayload());
//Log.w(TAG, "readFromNFC:: "+message);
if (mEvents != null){
Log.d(TAG, "mEvents aren't null");
mEvents.success(message);
}
} else {
if (mEvents != null){
mEvents.error("ERROR", "empty", null);
}
}
ndef.close();
這是預期的行為嗎?
這種行為沒有什么奇怪的。
如果“怪異地掃描”意味着您在繼續訪問標簽時(例如,在調用connect()
之前connect()
掃描標簽並將其拉出讀取范圍,則應獲得IOException
,如此處所述 。 之所以會出現IO異常,是因為您嘗試訪問不再可用的外部設備(NFC標簽)。 因此,無法再進行通信,並且必須中斷IO操作。
但是,當您只想從標記中讀取NDEF消息時,在將標記發現事件發送到您的應用之前,Android已經為您解決了這一問題。 Android通過意向額外的EXTRA_NDEF_MESSAGES將該NDEF消息傳遞到您的應用程序。 因此,(通常)無需再次手動讀取NDEF消息(除非標簽上的消息動態更改)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.