在此處輸入影像描述 我是從事 NFC 閱讀器專案的新手 Java 程式員。我一直在嘗試在 Android 中讀取 NFC 卡的內容,但無法正常作業。我只能檢索 NFC 卡的 UID。我瀏覽了 Android 中的 NFC 檔案以及一些教程,但我不太了解。我搜索了很多,但我沒有一個明確的解決方案或關于閱讀 Mifare Classic 1K 文本記錄的文章。我怎樣才能做到這一點?真的我什么都不知道,所以如果問題有點不清楚,請原諒我。我正在使用 NFC 工具桌面應用程式撰寫文本記錄(下面的螢屏截圖),我將不勝感激。提前致謝
這是我在獲得android意圖后用來獲取記錄的代碼片段
private void readFromIntent(Intent intent) {
System.out.println("Came huered ");
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] messages ;
if (rawMessages != null) {
messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i ) {
messages[i] = (NdefMessage) rawMessages[i];
NdefRecord [] records = messages[i].getRecords();
System.out.println("RECORDS " records);
//if you are sure you have text then you don't need to test TNF
for(NdefRecord record: records){
processRecord(record);
}
}
}
}
}
public void processRecord(NdefRecord record) {
short tnf = record.getTnf();
switch (tnf) {
case NdefRecord.TNF_MIME_MEDIA: {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (record.toMimeType().equals("MIME/Type")) {
// handle this as you want
System.out.println("HEREEEE");
} else {
//Record is not our MIME
}
}
}
// you can write more cases
default: {
//unsupported NDEF Record
}
}
}
uj5u.com熱心網友回復:
您對記錄的處理不是在尋找 Ndef 文本記錄。
case NdefRecord.TNF_MIME_MEDIA: {
不適合文本記錄
采用
short tnf = record.getTnf();
byte[] type = record.getType();
if (tnf == NdefRecord.TNF_WELL_KNOWN &&
Arrays.equals(type, NdefRecord.RTD_TEXT) {
// Correct TNF and Type for Text record
// Now process the Text Record encoding
}
請注意,toMimeType實際上確實轉換RTD_TEXT為 mime 型別,但為此
if (record.toMimeType().equals("MIME/Type")) {
需要是
if (record.toMimeType().equals("text/plain")) {
有關處理文本記錄,請參閱https://stackoverflow.com/a/59515909/2373819
更新
也應該說
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
錯誤,因為當標簽中沒有 Ndef 資料時,您試圖決議 Ndef 資料。
應該
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
update2 我查看了您提供的代碼的 github 鏈接
它不會在 Ndef 記錄上觸發,因為您正在測驗錯誤型別的 Intent
Intent 調度系統僅發送最高型別的 NFC Intent,其順序如下:-
ACTION_NDEF_DISCOVERED -> ACTION_TECH_DISCOVERED -> ACTION_TAG_DISCOVERED
因此,如果標簽包含 NDEF 資料,則僅設定 ACTION_NDEF_DISCOVERED 的操作型別。
因此
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
...
永遠不會觸發 Intent 的處理
該代碼應該是
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476544.html
上一篇:如何保持設備喚醒?
