Question

I have a problem sending an SMS to different carriers: a "@" is translated to "?". My message is encoded in UTF-8, I use this code:

SmsManager manager = SmsManager.getDefault();   
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);

String message = new String(new String("@").getBytes("UTF-8"))
manager.sendTextMessage(phonenumber, null,  message , piSend, piDelivered);

Thanks!

Note:

I tried to use the code below but I have a problem. On some OS like Windows phone 8, the SMS is never delivered.

manager.sendDataMessage(phonenumber, null, (short) 8091, message.getBytes(), piSend,     
piDelivered);
Was it helpful?

Solution

The third parameter of sendTextMessage() is an ordinary String. There is no need to convert a String to a UTF-8 encoded byte[] array and then back to a String again (especially since you are not doing that last conversion correctly). Get rid of getBytes() altogether and just use "@" as-is:

String message = "@";
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);

Or simply:

manager.sendTextMessage(phonenumber, null, "@", piSend, piDelivered);

It does make sense to use getBytes() when using sendDataMessage() instead.

Update: doing some research, I see that SMS can use one of three different character encodings. In GSM-03.38 encoding, a @ character is usually encoded as octet 0x00, not as octet 0x40 like it is in standard ASCII-compatible charsets. So that might be causing some issues for your carriers if your device is using GSM encoding internally. You may have to use sendDataMessage() instead so you can encode the String data to UTF-16, if your device does not automatically handle that for you (I would expect Android to handle that internally, but who knows if it really does). You might have to do that anyway if you want to send international or other non-alphanumeric characters:

manager.sendDataMessage(phonenumber, null, 8091, message.getBytes("UTF-16"), piSend,     

piDelivered);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top