简体   繁体   English

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

[英]NFC stops working after writing on iCODE tags, Android 6.0

I'm developing an android application to read and and write different NFC tags. 我正在开发一个Android应用程序来读取和写入不同的NFC标签。 I've encountered a problem with a specific tag ,iCODE SLI X and iCODE SLI S. After i write information on the tag, i'm not able to do any other action, looks like NFC stops working correctly because if i restart it, it will actually read the tag. 我遇到了特定标签的问题,iCODE SLI X和iCODE SLI S.在我写了关于标签的信息后,我无法做任何其他动作,看起来NFC停止正常工作,因为如果我重新启动它,它实际上会读取标签。 This does not happen if i use another tag type like MIFARE Classic 1K. 如果我使用另一种标签类型,如MIFARE Classic 1K,则不会发生这种情况。 Android version is 6.0. Android版本是6.0。

On the other hand, if i try the application on another device with Android 6.1 or 7.0 (exact same code), iCODE SLI X and iCODE SLIS will work okay, but not MIFARE Classic 1K. 另一方面,如果我在使用Android 6.1或7.0(完全相同的代码)的另一台设备上试用该应用程序,iCODE SLI X和iCODE SLIS将正常工作,但不适用于MIFARE Classic 1K。

Besides trying different samples of codes, i have also tried 2 applications on these devices. 除了尝试不同的代码示例,我还在这些设备上尝试了2个应用程序。 On "NFC Tools" you can see exactly the same problems that i have on my application. 在“NFC工具”上,您可以看到与我的应用程序完全相同的问题。 "TagWriter" from NXP is the only application that works like a charm with all types of tags. 来自恩智浦的“TagWriter”是唯一一款适用于所有类型标签的魅力的应用程序。

Here is the code I'm using to write the information on the tag: 这是我用来编写标签信息的代码:

@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){}
        }
    }
}

I can't understand what I'm possibly doing wrong ... 我无法理解我可能做错了什么......

I managed to understand what was wrong with my application, so I'm posting the answer myself. 我设法理解我的应用程序出了什么问题,所以我自己发布了答案。 Here is the thing : 这是事情:

When i try to write the information on the tag I first check if the tag is formatted to use "Ndef" technology on it, if not I use "NdefFormatable" to format the tag. 当我尝试在标签上写入信息时,我首先检查标签是否格式化为使用“Ndef”技术,如果不是,我使用“NdefFormatable”格式化标签。

The strange thing is, a certain tag in some devices supports "NdefFormatable" and in some it doesn't. 奇怪的是,某些设备中的某个标签支持“NdefFormatable”,而某些设备则不支持。 (not sure if its related to NFC itself or the OS version). (不确定它是否与NFC本身或OS版本有关)。 This was causing NFC to misbehave or not to work at all after I tried to use "NdefFormatable". 在我尝试使用“NdefFormatable”之后,这导致NFC行为不端或根本不工作。

What I'm doing now is that i have build this function that gives the technologies that I can use on the tag. 我现在正在做的是我已经构建了这个函数,它提供了我可以在标签上使用的技术。 Depending on it, I use "NdefFormatable" or "NfcV" (for iCODE tags) to read or write on the tag. 根据它,我使用“NdefFormatable”或“NfcV”(对于iCODE标签)来读取或写入标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM