Search code examples
androidnfcmifare

NFC Android: getting value from a NdefMessage


I'm messing around with some tags (Mifare classic)

I have written with the nxp application tagwriter an message in plaintext to the tag.

the following code is what I have so far:

 ** Called when a new nfc interaction/intent is fired */
public void onNewIntent(Intent intent) {
    NdefMessage[] msgs = null;
    if(intent.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if(rawMsgs!=null){
            msgs = new NdefMessage[rawMsgs.length];
            for(int i=0;i<rawMsgs.length;i++){
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
    } else {
        Log.e(TAG, "Other intent then NDEF_DISCOVERED");
    }

I dont know where to go one from this point, can someone point me in the right direction?

I want to read the value inside these messages, I already know that a ndefmessage contains ndefrecords, but how can i determine which record is the record i need?


Solution

  • A simple example for the first record of the first message:

    NdefRecord[] recs = msgs[0].getRecords();
    byte[] type = recs[0].getType();
    byte[] value = recs[0].getPayload();
    

    When you know the type, you may be able to interpret the value bytes.