androidnfcmifarendef

Get NDEF-Messages from block data


I have an android NFC solution based in NDEF-Messages where I read parcelable extra tags from android intent. The data in NFC card is text plane and not key protected.

I need run my app on a personalized hardware, the owner brings to me an SDK for that purpose, but not support NDEF-Messages as native android do.

The owner sdk have authenticate(int block, int keyType, byte[] key) method, and read_block(int block). So, I try to read all information from blocks and convert that to NDEF-Messages but I was not successful.

How I can convert read data to NDEF-Messages? Or extract only the payload data?

my source code is:

StringBuilder result = new StringBuilder();
for(int i=0;i<64;i+=4){
   if(util.authenticate(i, 0, key)){
      for(int j=i;j<i+4;j++){
         byte[] readData = util.read_block(i);
         if(readData != null){
            result.append(NfcUtil.toHexString(readData));
         }
      }
   }
}

I need get "hello world" text data

enter image description here enter image description here


Solution

  • NDEF data format can take a number of forms depending on the NFC Tag standard used to store it.

    You don't tell us how the Ndef data was encoded and stored on the tag which makes it is difficult to answer as the are many different ways it could be done.

    BUT as a guess and that you have Tagged mifare then it might follow the NXP's proprietary method of storing Ndef Data on Mifare Classic Tags which is defined in the doc AN1305

    Update based on new data

    So that data more looks like the standard Type2 Ndef format.

    The spec is available here

    But basically you would read block 04 to get 03h,12h,D1h,01h

    03h is the Ndef Tag of the NDEF TLV and 12h (18 in Decimal) so you need to read 18 bytes after the 12h to get the Ndef message and should then read another byte to get the FEh Terminator TLV

    You have the first 2 bytes in the block 04, so you need to read another (18 - 2) / 4 = 4 blocks.

    This matches the data shown.

    These 18 bytes are the complete Ndef message as you wanted (You would then need to extract the Ndef Record containing your data and then parse the Ndef Record to get your text, this can be done using standard Android class) If not the NDef message structure is here and then how to decode the records are here

    For decoding the Record payload, if you look at block 05 0Eh,54h,02h,65h the 54h means its a RTD_TEXT Record and 02h is 2 bytes of the language type definition which gives you "en" for english, then your message "hello world" begins.