Character problems coming in Ndefmessage NFC

Asked

Viewed 89 times

2

I am making a program that exchanges messages for NFC, and I am having a problem that when I want to pass a Parameter in the message appears all right now when I want to pass 2 parameters, so there is one that always appears with characters this in the Tlm that receives the message.

I’ll show you my source.

Code of the class sending 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 NFC message:

   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 the UTF-8 and appears characters that should not and the other appears in the UTF-16 and appears all right.

Somebody help me with this problem?

1 answer

0


statusByte seems to be being used before being assigned in Decoder. Since the declared variable starts with zero, the Decoder must be assuming UTF-16 in the first round.

I don’t like this math either:

           int isUTF8=statusByte-languageCodeLength;

but it 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 from 5 actually reaches the value -0x80.

  • Thank you so much for that, it worked. So long I’ve been lost here with this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.