在 iCODE 标签上写入后 NFC 停止工作,Android 6.0

NFC stops working after writing on iCODE tags, Android 6.0

我正在开发一个 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 不行。

除了尝试不同的代码示例外,我还在这些设备上尝试了 2 个应用程序。在 "NFC Tools" 上,您可以看到与我的应用程序完全相同的问题。来自 NXP 的 "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){}
        }
    }
}

我不明白我可能做错了什么......

我设法了解我的应用程序出了什么问题,所以我自己发布了答案。事情是这样的:

当我尝试在标签上写入信息时,我首先检查标签是否被格式化为使用 "Ndef" 技术,如果不是,我使用 "NdefFormatable" 来格式化标签。

奇怪的是,某些设备中的某个标签支持 "NdefFormatable",而有些则不支持。 (不确定它是否与 NFC 本身或 OS 版本有关)。在我尝试使用 "NdefFormatable".

后,这导致 NFC 行为异常或根本无法工作

我现在正在做的是我已经构建了这个功能,它提供了我可以在标签上使用的技术。根据它,我使用"NdefFormatable"或"NfcV"(对于iCODE标签)在标签上读取或写入。