2

I'm facing problem when I try to erase data in NFC tag. I'm using MifareUltralight type NFC tag. Please some one help me to find solution. Here is my code. I got this code from here

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
    mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}

NdefFormatable formatable = NdefFormatable.get(mytag);

if (formatable != null) { // Here I'm getting formatable null
    try {
        formatable.connect();

        try {
            formatable.format(methodGetMsg());
        } catch (Exception e) {
            // let the user know the tag refused to format
        }
    } catch (Exception e) {
        // let the user know the tag refused to connect
    } finally {
        try {
            formatable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} else {
    // let the user know the tag cannot be formatted
}
Community
  • 1
  • 1
Aristo Michael
  • 2,166
  • 3
  • 35
  • 43
  • 1
    possible duplicate of [Not able to format NFC card using NdefFormatable](http://stackoverflow.com/questions/25512204/not-able-to-format-nfc-card-using-ndefformatable) – Michael Roland Oct 15 '14 at 05:53

4 Answers4

3

I think you may not be understanding what formatting means in the context of NDEF messages on tags. The exact methodology varies from tag to tag, but at the end of the day 'formatting' a tag for NDEF merely consists of putting the NDEF magic number and some metadata in a certain spot.

In the case of a MIFARE Ultralight, the magic number is placed in one-time programmable memory, so it's actually not possible to format an Ultralight for NDEF after you've already formatted it, although you can write different NDEF messages to it assuming the tag is not locked. In general, Android devices seem to recognize this and will not let you use NdefFormatable with Ultralights that are already formatted. As for other tags, it's highly inconsistent if a NDEF formatted tag will enumerate NdefFormatable even if it's an unlocked Topaz or other tag where it is possible to write the formatting bits again.

If you want to overwrite NDEF data with Android, your most reliable bet is to use Ndef to overwrite the existing Ndef message with a new one.

jlvo
  • 111
  • 3
3

As your tag already lists the android.nfc.tech.Ndef technology, it is already prepared to store an NDEF message and does not need further formatting. You can simply overwrite (given that the tag is not read-only) any existing NDEF message by using the writeNdefMessage() method of the Ndef object. E.g. to "format" the tag to an empty NDEF message, you could do something like:

Ndef ndefTag = Ndef.get(tag);
ndefTag.writeNdefMessage(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)));

Answer taken from here Thanks to Michael Roland

Community
  • 1
  • 1
Aristo Michael
  • 2,166
  • 3
  • 35
  • 43
1

It might be that your device does not support MifareUltralight formatting.

It is stated in the developer documentation that there is no mandatory set of tags for which all Android devices with NFC must support NdefFormatable 1.

This is because tag formatting to make it NDEF compatible often requires proprietary knowledge 2.

Community
  • 1
  • 1
araks
  • 40,738
  • 8
  • 34
  • 39
0

How to reset the card, modify the format ndef of the card to the original format

  • This seems like a follow up question to the original question. It will be better if this can be posted as a separate question. – NSaran Jun 15 '22 at 22:59