2012-11-12 44 views
0

我目前正在开发一个应用程序,它会读取NFC tags目前我已经写代码即可获得TAG id什么,我要做什么?我怎样才能读取所有数据,如果intent额外命名EXTRA_NDEF_MESSAGES是空的。Android的读取NFC标签数据

读取RFID我对现在的代码是

public void onNewIntent(Intent intent) { 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

     String zin = tag.getTechList()[0]; 
     info.setText("TagID: " + bytesToHex(tag.getId())+" Saturs: "+zin);  

} 

我想知道如何在NFC tag读取所有数据。

谢谢媒体链接!

+0

我很困惑,你读RFID或NFC标签? – ThomasRS

+0

@Thomas对此抱歉,我想阅读RFID。 – J1and1

+0

那你为什么使用NfcAdapter? (如在NFC!= RFID) – ThomasRS

回答

0

取决于你的标签的类型。对于超轻MIFARE一个例子

Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
MifareUltralight uTag = MifareUltralight.get(tagFromIntent); 
uTag.connect(); //You should enclose this into a try-catch because of probably IOException 
byte[] data = uTag.readPages(INDEX_OF_PAGES_YOU_WANT); //This returns 4 consecutive pages from the offset you declared. Each page weights 4 bytes 
uTag.close(); 
+0

谢谢!那只是做了我需要的东西:D – J1and1

0

有多种类型的NFC标签(例如,超轻的Mifare,超轻的Mifare C,MIFARE经典,FeliCa的......)的。 每个标签有不同的内存大小和阅读程序。 例如:超轻型的Mifare有64字节,但MIFARE经典1K含有1千字节存储器。 要从mifare ultratralight读取数据,不需要额外的认证,但Mifare classic需要认证。 当你得到新的意向可以分析它来获取标记信息:

@Override 
    protected void onNewIntent(Intent intent){  
     getTagInfo(intent) 
     } 
    private void getTagInfo(Intent intent) { 
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
    String[] techList = tag.getTechList(); 
    for (int i = 0; i < techList.length; i++) { 
    if (techList[i].equals(MifareClassic.class.getName())) { 

     MifareClassic mifareClassicTag = MifareClassic.get(tag); 
     switch (mifareClassicTag.getType()) { 
     case MifareClassic.TYPE_CLASSIC: 
      //Type Clssic 
      break; 
     case MifareClassic.TYPE_PLUS: 
      //Type Plus 
      break; 
     case MifareClassic.TYPE_PRO: 
      //Type Pro 
      break; 
     } 
    } else if (techList[i].equals(MifareUltralight.class.getName())) { 
    //For Mifare Ultralight 
     MifareUltralight mifareUlTag = MifareUltralight.get(tag); 
     switch (mifareUlTag.getType()) { 
     case MifareUltralight.TYPE_ULTRALIGHT: 
      break; 
     case MifareUltralight.TYPE_ULTRALIGHT_C: 

      break; 
     } 
    } else if (techList[i].equals(IsoDep.class.getName())) { 
     // info[1] = "IsoDep"; 
     IsoDep isoDepTag = IsoDep.get(tag); 

    } else if (techList[i].equals(Ndef.class.getName())) { 
     Ndef.get(tag); 

    } else if (techList[i].equals(NdefFormatable.class.getName())) { 

     NdefFormatable ndefFormatableTag = NdefFormatable.get(tag); 

    } 
    } 
    } 
    } 

当你得到确切的标签,然后你要开始阅读程序该标签。 阅读标签完整的项目在这里My GitHub Repo