2013-07-01 89 views
4

我正在研究一種可在NFC標籤上讀寫的android應用程序。 閱讀我已經寫了一些東西的標籤沒有問題,但是當我使用空白標籤時,我很難在HEX代碼中讀取標籤的UID。Android NFC java.io.IOException:收發失敗

我正在使用mifare經典標籤,並且直接讀取帶有readblock方法的十六進制UID。奇怪的是,它在我得到UID的調試器模式下工作正常。但是,當我試圖不debbuger我得到以下異常:

java.io.IOException: Transceive failed 

這裏是我的方法來讀取到標籤:

static String getUID(Intent intent) { 

    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
    MifareClassic mc = MifareClassic.get(tagFromIntent); 

    try { 
     mc.connect(); 
     Log.i("connect", "ok"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Log.i("connect", "nok"); 
     e.printStackTrace(); 
    } 
    try { 
     boolean secA = mc.authenticateSectorWithKeyA(0, mc.KEY_DEFAULT); 
     Log.i("secA", "ok"); 
    } catch (IOException e) { 
     Log.i("secA", "nok"); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     boolean secB = mc.authenticateSectorWithKeyB(0, mc.KEY_DEFAULT); 
     Log.i("secB", "ok"); 
    } catch (IOException e) { 
     Log.i("secB", "nok"); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    byte[] uidBytes = null; 

    try { 

     uidBytes = mc.readBlock(0); 
     Log.i("bytes", "ok"); 

    } catch (IOException e) { 
     Log.i("bytes", "nok"); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 

     mc.close(); 
     Log.i("close", "ok"); 
    } catch (IOException e) { 
     Log.i("close", "nok"); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if (uidBytes != null) { 
    String uid = HexToString(uidBytes); 

    return uid; 
    } 
    else { return "Repasser le tag";} 
} 

我不知道如何解決這個問題,因爲它工作在調試模式。

+0

如何使用'Log.d()'方法來找出調試和運行之間的區別?另外,你可以顯示你的broadcastreceiver的'AndroidManifest.xml'聲明嗎? – tolgap

回答

0

可能存在身份驗證問題。 您可以通過這種方式在這裏驗證這一點....

if (mfc.authenticateSectorWithKeyA(sectorNumber, 
       MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) { 
      Log.d("TAG", "Authorized sector with MAD key"); 

     } else if (mfc.authenticateSectorWithKeyA(
       sectorNumber, MifareClassic.KEY_DEFAULT)) { 
      Log.d("TAG", 
        "Authorization granted to sector with DEFAULT key"); 

     } else if (mfc 
       .authenticateSectorWithKeyA(sectorNumber, 
         MifareClassic.KEY_NFC_FORUM)) { 
      Log.d("TAG", 
        "Authorization granted to sector with NFC_FORUM key"); 

     } else { 
      Log.d("TAG", "Authorization denied "); 

      return false; 
     } 

SectorNumber是:要進行身份驗證的部門。例如:0,1,2 .... 15用於mifare Classic 1K 驗證完成後可以讀取或寫入。

1

此代碼適用於我。在讀取塊之前,您必須檢查身份驗證。

MifareClassic mif = MifareClassic.get(detectedTag); 

int ttype = mif.getType(); 
Log.d(TAG, "MifareClassic tag type: " + ttype); 

int tsize = mif.getSize(); 
Log.d(TAG, "tag size: " + tsize); 

int s_len = mif.getSectorCount(); 
Log.d(TAG, "tag sector count: " + s_len); 

int b_len = mif.getBlockCount(); 
Log.d(TAG, "tag block count: " + b_len); 
try { 
    mif.connect(); 
    if (mif.isConnected()){ 

     for(int i=0; i< s_len; i++){ 

      boolean isAuthenticated = false; 

      if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) { 
       isAuthenticated = true; 
      } else if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)) { 
       isAuthenticated = true; 
      } else if (mif.authenticateSectorWithKeyA(i,MifareClassic.KEY_NFC_FORUM)) { 
       isAuthenticated = true; 
      } else { 
       Log.d("TAG", "Authorization denied "); 
      } 

      if(isAuthenticated) { 
       int block_index = mif.sectorToBlock(i); 

       byte[] block = mif.readBlock(block_index); 
       String s_block = NfcUtils.ByteArrayToHexString(block); 
       Log.d(TAG, s_block); 
      } 
     } 
    } 
    mif.close(); 

} catch (IOException e) { 
    e.printStackTrace(); 
} 
相關問題