[英]Android read NFC tag data
我目前正在開發一個將讀取NFC tags
的應用程序,目前我已經編寫了獲取TAG id
的代碼,下一步該怎么做? 如果名為EXTRA_NDEF_MESSAGES
額外intent
為空,我如何讀取所有數據。
我現在擁有的讀取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
所有數據。
謝謝大家!
nfc標簽有各種類型(例如Mifare Ultralight,Mifare Ultralight C,Mifare Classic,felica ...)。 每個標簽具有不同的內存大小和讀取過程。 例如:Mifare Ultralight具有64字節,但mifare Classic 1k包含1 KB內存。 要從mifare ultralight讀取數據,不需要額外的身份驗證,但是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中
取決於您的TAG的類型。 超輕型惡作劇的例子
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.