我正在開發一個android應用程序來讀取和寫入不同的NFC標籤。我遇到了特定標籤,iCODE SLI X和iCODE SLI S的問題。在我在標籤上寫入信息後,我無法做任何其他操作,看起來NFC停止正常工作,因爲如果我重新啓動它,它會實際讀取標籤。如果我使用其他標籤類型(如MIFARE Classic 1K),則不會發生這種情況。 Android版本是6.0。另一方面,如果我在Android 6.1或7.0(完全相同的代碼)的其他設備上嘗試應用程序,iCODE SLI X和iCODE SLIS將可以正常工作,但不是MIFARE Classic 1K。NFC在iCODE標籤上寫入後停止工作,Android 6.0
除了嘗試不同的代碼示例,我也嘗試過在這些設備上的2個應用程序。在「NFC工具」上,您可以看到與我的應用程序完全相同的問題。恩智浦的「TagWriter」是唯一一款適用於所有類型標籤的應用程序。
下面是我用寫在標籤上的信息代碼:
@Override
protected void onNewIntent(Intent intent) {
if (mNfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
try {
Ndef ndef = Ndef.get(tag);
NdefRecord text1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
youstring1.getBytes(Charset.forName("US-ASCII")),
null,
youstring1.getBytes());
NdefRecord text2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
youstring2.getBytes(Charset.forName("US-ASCII")),
null,
youstring2.getBytes());
NdefRecord[] records = {text1, text2};
NdefMessage message = new NdefMessage(records);
if (ndef != null) {
NdefMessage ndefMesg = ndef.getCachedNdefMessage();
if (ndefMesg != null) {
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
}
} else {
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable != null) {
// initialize tag with new NDEF message
try {
ndefFormatable.connect();
ndefFormatable.format(message);
ndefFormatable.close();
} finally {
try {
//ndefFormatable.close();
} catch (Exception e) {
}
}
}
}
}catch (FormatException |IOException ue){}
}
}
}
我不明白我可能做錯了......