Problems with character that comes in NDefMessage NFC

2

I'm doing a program that exchanges messages by NFC, and I'm having a problem that when I want to pass a Parameters in the message everything seems correct now when I want to pass 2 parameters, then there is one that always appears with characters this in the tlm that you receive the message.

I'll show you my source.

Code of the class that Sends the message:

    NdefMessage create_RTD_TEXT_NdefMessage(String cumprimento,String ID)
{

    Locale locale = new Locale("en", "US");
    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));

    boolean encodeInUtf8 = false;
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    byte status = (byte) (utfBit + langBytes.length);

    byte[] textBytes = cumprimento.getBytes(utfEncoding);
    byte[] data = new byte[1 + langBytes.length + textBytes.length];

    data[0] = (byte) status;
    System.arraycopy(langBytes, 0, data, 1, langBytes.length);
    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);


    Locale locale2 = new Locale("en", "US");
    byte[] langBytes2 = locale2.getLanguage().getBytes(Charset.forName("US-ASCII"));
    boolean encodeInUtf8_ = false;
    Charset utfEncoding2 = encodeInUtf8_ ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    int utfBit2 = encodeInUtf8_ ? 0 : (1 << 7);
    byte status2 = (byte) (utfBit2 + langBytes2.length);

    byte[] textBytes2 = ID.getBytes(utfEncoding2);
    byte[] data2 = new byte[1 + langBytes2.length + textBytes2.length];

    data[0] = (byte) status2;
    System.arraycopy(langBytes2, 0, data2, 1, langBytes2.length);
    System.arraycopy(textBytes2, 0, data2, 1 + langBytes2.length, textBytes2.length);


    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data2);

    NdefMessage message = new NdefMessage(textRecord, textRecord2);

    return message;

}

And now the class that receives the message by NFC:

   void processIntent(Intent intent) {

         NdefMessage[] messages = getNdefMessages(getIntent());
         for(int i=0; i <messages.length; i++){
             for(int j=0;j< messages[0].getRecords().length;j++){

                NdefRecord record = messages[i].getRecords()[j];
                 int languageCodeLength= statusByte & 0x3F; //mask value in order to find language code length 
                 int isUTF8=statusByte-languageCodeLength;

                if(j == 0)
                    {
                         statusByte=record.getPayload()[0];

                         if(isUTF8==0x00){
                            ammount=new String(record.getPayload(),1+languageCodeLength,record.getPayload().length-1-languageCodeLength,Charset.forName("UTF-8"));
                            System.out.println(ammount + "***********");
                         }
                         else if (isUTF8==-0x80){
                            cumprimento = new String(record.getPayload(),1+languageCodeLength,record.getPayload().length-1-languageCodeLength,Charset.forName("UTF-16"));
                         }
                    }

                if(j == 1)
                     {
                         statusByte1 = record.getPayload()[0];

                         if(isUTF8==0x00){
                            id = new String(record.getPayload(),1+languageCodeLength,record.getPayload().length-1-languageCodeLength,Charset.forName("UTF-8"));
                         }
                         else if (isUTF8==-0x80){
                            id = new String(record.getPayload(),1+languageCodeLength,record.getPayload().length-1-languageCodeLength,Charset.forName("UTF-16"));

                         }
                    }
                Valor = (TextView) this.findViewById(R.id.Valor ); 
                Valor.setText("Montante a pagar \n" + "€ "  + cumprimento);
             }
         }
    }

The problem that is happening is that one of the values in the IF goes through UTF-8 and appears characters that should not and the other one appears in UTF-16 and everything appears ok.

Does anyone help me with this problem?

    
asked by anonymous 25.02.2014 / 17:06

1 answer

0

statusByte seems to be in use before being assigned to the decoder. Since the declared variable starts with a zero value, the decoder must be presuming UTF-16 in the first round.

This math here does not please me too much:

           int isUTF8=statusByte-languageCodeLength;

but seems to be correct. I simulated here with a UTF-8 string of length 5 would be 0x85, when converted to integer it turns negative -0x7b, which decreased by 5 actually reaches the value -0x80.     

02.03.2014 / 17:40