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?