Search code examples
javaandroidnfc

How to use android to get complete data in NFC tag?


I wrote this program which can read all payload in a NFC tag

@Override
protected void onNewIntent(Intent intent) {

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

        if (rawMsgs != null) {
            String payloadString="";
            NdefMessage[] msg = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msg[i] = (NdefMessage) rawMsgs[i];
                NdefRecord[] rec=msg[i].getRecords();
                byte[] byteArray=rec[0].getPayload();
                payloadString=new String(byteArray);

                Log.v("log", payloadString);
            }
        }
    }
}

I tried to change NfcAdapter.EXTRA_NDEF_MESSAGES to different value, such as:

NfcAdapter.EXTRA_ADAPTER_STATE, 
NfcAdapter.EXTRA_TAG, 
NfcAdapter.EXTRA_ID

But no one can get data from tag

If use some NFC reader app to read the tag, you will see there are some data before and after the payload, this is the complete data in the tag.

ntag203-1

ntag203-2

ntag203-3

the type of tag ic is NTAG203

(05) D1 01 85 54 is the header of data.

from (06) 02 65 6E 30 30 30 30 ..... to (27) 30 30 30 31 is the content of payload

I want to get all data in the tag, such as:

from (00) 04 DB 39 64 ... to (05) 44 03 89

and (28) 00 00 -- -- , (29) 00 00 -- --

How to use android to read these data?

The purpose is, use these data to protect my product, I will ask manufacturer to write some key to these readonly area, only the NFC card have the key can use my app, so I need to read these data.


Solution

  • The Android NDEF APIs will give you the NDEF content of the tag, and abstract all the details about the tag technology.

    If you want to read raw data from the tag, you need to use android.nfc.tech.* classes for that particular tag type you're reading. In this case NTAG203 is Type 2 Mifare Ultralight.

    You can use readPages(page) to get the raw data from the tag.