Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format an NFC tag in NDEF format

Tags:

android

nfc

I've a Mifare Classic1K NFC tag but I'm unable to write any content over it. Its writable but seems like it is not formatted in NDEF which is a pre-requisite for Android devices to be write data on it. Any suggestion is welcome.

P.S: I do have a TRF7960 RF Antenna if that can help to format it.

like image 644
Panshul Avatar asked Dec 20 '12 11:12

Panshul


People also ask

What is NDEF NFC tag?

NDEF is a standardized data format specification by the NFC Forum which is used to describe how a set of actions are to be encoded onto a NFC tag or to be exchanged between two active NFC devices. The vast majority of NFC enabled devices (readers, phones, tablets…) support reading NDEF messages from NFC tags.

Can you reprogram NFC tags?

NFC Tags are rewritable by default. Potentially, the NFC Tag can be rewritten endlessly. They are guaranteed to be rewritten up to 100,000 times (depending on the IC). However, you can also block them, so that they will no longer be rewritten.

What kind of data is contained in NDEF?

An NDEF record contains a payload of data and metadata describing how to interpret the payload. Each record's payload can be one of several different data types. The header for each record contains metadata describing the record and its place in the message, followed by its type and ID.


1 Answers

Given an android.nfc.Tag object named tag, to format it, use:

    NdefFormatable formatable=NdefFormatable.get(tag);

    if (formatable != null) {
      try {
        formatable.connect();

        try {
          formatable.format(msg);
        }
        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 {
        formatable.close();
      }
    }
    else {
      // let the user know the tag cannot be formatted
    }
like image 131
CommonsWare Avatar answered Nov 15 '22 11:11

CommonsWare