简体   繁体   中英

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. 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. This does not happen if i use another tag type like MIFARE Classic 1K. Android version is 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.

Besides trying different samples of codes, i have also tried 2 applications on these devices. On "NFC Tools" you can see exactly the same problems that i have on my application. "TagWriter" from NXP is the only application that works like a charm with all types of tags.

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.

The strange thing is, a certain tag in some devices supports "NdefFormatable" and in some it doesn't. (not sure if its related to NFC itself or the OS version). This was causing NFC to misbehave or not to work at all after I tried to use "NdefFormatable".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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