2015-04-20 61 views
0

我想格式化Mifare Ultralight C以處理NDEF消息。我已經編寫了下面的代碼,它在Mifare Classic 1K芯片上工作得很好,但是當我嘗試連接到Ultralight C芯片時,它會得到一個IOExeption。將Mifare Ultralight C格式化爲NDEF

NdefFormatable format = NdefFormatable.get(tag) 

    if(format != null){ 
     try { 
      format.connect(); 
      format.format(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null))); 
      format.close(); 
      Toast.makeText(getApplicationContext(), "Tag formated.", Toast.LENGTH_LONG).show(); 
     }catch (IOException e) { 
      // TODO Auto-generated catch block 
      Toast.makeText(getApplicationContext(), "Failed to connect", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 

     } catch (FormatException e) { 
      // TODO Auto-generated catch block 
      Toast.makeText(getApplicationContext(), "Failed Format", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 
    else 
     Toast.makeText(getApplicationContext(), "Tag unformatable or already formatted to Ndef.", Toast.LENGTH_LONG).show(); 

} 

有誰知道錯誤可能是什麼,爲什麼代碼適用於1k芯片而不是Ultralight C?該標籤是全新的並且完全空白。

這是logcat的輸出:

04-21 08:49:27.300: W/System.err(9351): java.io.IOException 
04-21 08:49:27.300: W/System.err(9351):  at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:132) 
04-21 08:49:27.300: W/System.err(9351):  at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:95) 
04-21 08:49:27.300: W/System.err(9351):  at com.example.exjobb.nfc.FormatTag.formatTag(FormatTag.java:69) 
04-21 08:49:27.300: W/System.err(9351):  at com.example.exjobb.nfc.FormatTag.onNewIntent(FormatTag.java:58 
04-21 08:49:27.300: W/System.err(9351):  at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1178) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2418) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread.performNewIntents(ActivityThread.java:2431) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2440) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread.access$1500(ActivityThread.java:159) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1387) 
04-21 08:49:27.300: W/System.err(9351):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-21 08:49:27.300: W/System.err(9351):  at android.os.Looper.loop(Looper.java:176) 
04-21 08:49:27.300: W/System.err(9351):  at android.app.ActivityThread.main(ActivityThread.java:5419) 
04-21 08:49:27.300: W/System.err(9351):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-21 08:49:27.300: W/System.err(9351):  at java.lang.reflect.Method.invoke(Method.java:525) 
04-21 08:49:27.305: W/System.err(9351):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
04-21 08:49:27.305: W/System.err(9351):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
04-21 08:49:27.305: W/System.err(9351):  at dalvik.system.NativeStart.main(Native Method) 
+0

請在logcat共享stacktrace – LaurentY

+0

這是一個空白的(即以前沒有使用的)超輕C標籤嗎? –

+0

@LaurentY 添加了logcat中的堆棧跟蹤。 –

回答

2

我通過添加其他過濾器到過濾器ForegroundDispatch所作的格式化工作。之前,我只是過濾標籤發現,但添加技術發現後,它似乎工作。

IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
IntentFilter[] nfcIntentFilter = new IntentFilter[]{tagDetected,techDetected}; 

nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, nfcIntentFilter, null); 

不要忘記將新的過濾器添加到您的onNewIntent方法。

相關問題