2013-06-26 43 views
0

我尝试当我运行测试编写一个程序来读取NCF tag.So的所有部门:测试NFC的Android只读12部门

  • 检测到标记
  • 我得到的标签ID
  • 我得到部门

的数量但我只能读取第一个扇区!

这里我的代码:

public class MainActivity extends Activity { 
    public String TAG_LOCAL = "Home - "; 
    public boolean MODE_DEBUG_LOCAL = true; 
    NfcAdapter nfcAdapter; 
    PendingIntent pendingIntent; 
    IntentFilter[] intentFilter; 
    String[][] generalTechLists; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.w("ISI", "Main Start"); 

     //detect if NFC device 
     if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) { 
      Log.w("ISI", "NFC detected"); 
     }else{ 
      Log.w("ISI", "NFC NOT detected"); 
     } 




     IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
     try { 
      ndef.addDataType("*/*"); 
     } catch (MalformedMimeTypeException e) { 
      Log.w("ISI", " error ndef = " + e.getMessage()); 
     } 
     intentFilter = new IntentFilter[] {ndef}; 

     generalTechLists = new String[][] { new String[] { MifareClassic.class.getName() } }; 

     nfcAdapter = NfcAdapter.getDefaultAdapter(this); 

     pendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     resolveIntent(intent); 
    } 

    void resolveIntent(Intent intent) { 

     String action = intent.getAction(); 
     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 
      //Identifiant du tag NFC 
      byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); 
      Log.w("ISI", "Tag id = " + ByteArrayToHexString(byte_id)); 

      //techno disponible 
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      String[] techList = tagFromIntent.getTechList(); 
      for (int Cpt = 0; Cpt <techList.length; Cpt++){ 
       Log.w("ISI", "Techno = " + techList[Cpt]); 
      } 


      //QUESTION A POSER DANS XDA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
      MifareClassic mfc = MifareClassic.get(tagFromIntent); 
      try{ 
       mfc.connect(); 
       int size = mfc.getSize(); 
       Log.w("ISI", "size (byte) = " + size); 
       int nbrSector = mfc.getSectorCount(); 
       Log.w("ISI", "NbrSector = " + nbrSector); 
       for (int Cpt = 0; Cpt < nbrSector; Cpt++){ 
        boolean auth = mfc.authenticateSectorWithKeyA(Cpt, MifareClassic.KEY_DEFAULT); 
        if (auth){ 


         int nbrBlock = mfc.getBlockCountInSector(Cpt); 
         Log.w("ISI", "NbrBlock = " + nbrBlock + " for sector " + Cpt); 
         for (int k = 0; k < nbrBlock; k++){ 
          byte[] data = mfc.readBlock(k); 
          Log.w("ISI", "Block num " + k + " - data " + ByteArrayToHexString(data)); 
         } 

        }else{ 
         Log.w("ISI", " Impossible to connect at sector " + Cpt); 
        } 
       } 
      }catch(IOException e){ 
       Log.w("ISI", " error = " + e.getMessage()); 
      } 


     }// End of method 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, generalTechLists); 
    } 


    private String getHexString(String hex){ 

     StringBuilder output = new StringBuilder(); 
     for (int i = 0; i < hex.length(); i+=2) { 
      String str = hex.substring(i, i+2); 
      output.append((char)Integer.parseInt(str, 16)); 
     } 
     return output.toString(); 
    } 


    String ByteArrayToHexString(byte[] inarray) { 
     //Thanks Adam Laurie sur XDA 
     int i, j, in; 
     String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
     String out= ""; 

     for(j = 0 ; j < inarray.length ; ++j) { 
      in = (int) inarray[j] & 0xff; 
      i = (in >> 4) & 0x0f; 
      out += hex[i]; 
      i = in & 0x0f; 
      out += hex[i]; 
     } 
     return out; 
    } 

} 

这里日志:

Tag id = C44F0EDD 
Techno = android.nfc.tech.MifareClassic 
Techno = android.nfc.tech.NfcA 
Techno = android.nfc.tech.NdefFormatable 
size (byte) = 1024 
NbrSector = 16 
NbrBlock = 4 for sector 0 
Block num 0 - data C44F0EDD58880400C185149849803612 
Block num 1 - data 00000000000000000000000000000000 
Block num 2 - data 00000000000000000000000000000000 
Block num 3 - data 000000000000FF078069FFFFFFFFFFFF 
NbrBlock = 4 for sector 1 
error = Tag was lost. 

所以我不明白发生了什么。

感谢帮助

回答

0

尝试改变:

for (int k = 0; k < nbrBlock; k++){ 
    byte[] data = mfc.readBlock(k); 
    Log.w("ISI", "Block num " + k + " - data " + ByteArrayToHexString(data)); 
} 

与此

byte[] data = mfc.readBlock(bIndex); 
for (int k = 0; k < nbrBlock; k++){ 
    bIndex = mfc.sectorToBlock(Cpt); 
    bIndex++; 
    Log.w("ISI", "Block num " + k + " - data " + ByteArrayToHexString(data)); 
} 

来自:http://mifareclassicdetectiononandroid.blogspot.it/

0

设置这样的代码:

int bIndex = 0; 
try{ 
    mfc.connect(); 
    int size = mfc.getSize(); 
    Log.i("ISI", "size (byte) = " + size); 
    int sectorCount = mfc.getSectorCount(); 
    Log.i("ISI", "NbrSector = " + sectorCount); 
    for (int Cpt = 0; Cpt < sectorCount; Cpt++){ 
     boolean auth = mfc.authenticateSectorWithKeyA(Cpt, MifareClassic.KEY_DEFAULT); 
     if (auth){ 
      int nbrBlock = mfc.getBlockCountInSector(Cpt); 
      Log.i("ISI", "NbrBlock = " + nbrBlock + " for sector " + Cpt); 
      for (int k = 0; k < nbrBlock; k++){ 
       byte[] data = mfc.readBlock(bIndex); 
       Log.i("ISI", "Block num " + k + " - data " + ByteArrayToHexString(data)); 
       bIndex++; 
      } 
     }else{ 
      Log.i("ISI", " Impossible to connect at sector " + Cpt); 
     } 
    } 
}catch(IOException e){ 
    Log.i("ISI", " error = " + e.getMessage()); 
}