4

Possible Duplicate:
Strange character on Android NDEF record payload

I am trying to read some plain text from NFC tag. and my codes are below;

public void processReadIntent(Intent intent){

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

    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    Log.d("msg", msg.getRecords()[0].getPayload().toString());

    String PatientId=new String(msg.getRecords()[0].getPayload());     
    String UserName="nurse";
    String Password="nurse";

   Toast.makeText(getApplicationContext(), PatientId, Toast.LENGTH_LONG).show();

    //tv.setText(new String(msg.getRecords()[0].getPayload()));
}

BUT, the problem here is when i read data i can see that my desired data has a 'en' at the start. for example: if my actual data in 'john', when i read, i can see it as 'enjohn'. I know 'en' is the language header. But how do I remove it??

i have tried with substring, but after that don't even work...

any idea about how to remove this language header???

Community
  • 1
  • 1
Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78

1 Answers1

9

Probably, you have the same problem here and how to read an NFC tag properly here

Snippet taken from 2nd link.

 try
{
        byte[] payload = record.getPayload();

        /*
     * payload[0] contains the "Status Byte Encodings" field, per the
     * NFC Forum "Text Record Type Definition" section 3.2.1.
     *
     * bit7 is the Text Encoding Field.
     *
     * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
     * The text is encoded in UTF16
     *
     * Bit_6 is reserved for future use and must be set to zero.
     *
     * Bits 5 to 0 are the length of the IANA language code.
     */

         //Get the Text Encoding
        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";

        //Get the Language Code
        int languageCodeLength = payload[0] & 0077;
        String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

        //Get the Text
        String text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);

    return new TextRecord(text, languageCode);
}
catch(Exception e)
{
        throw new RuntimeException("Record Parsing Failure!!");
}
Community
  • 1
  • 1
PCoder
  • 2,165
  • 3
  • 23
  • 32