I am developing a crypted sms app, I am crypting the sms to 3DES and I am trying to send the byte array result to the other phone using SmsManager.sendDataMessage but I am not able to read the right byte array in the receiver phone, for example:
My app sends the array:
[0,1,2,3,4,5,6,7,-100,-101,-105,-110]
and in the other phone, I receive only (if lucky)
[0,1,2,3,4]
I have tried sending as text with many encodings but it is not working.
I tried it on simulators and in devices and the behaviour is the same.
I tried sending bytes from printable characters (letters, numbers) and it works perfectly, so I think the problem are the non-printing bytes, but I need to send the bytes trough sms.... a friend made something like that but using blackberry and it worked fine.
What am I doing wrong?
This is the code for sending:
SmsManager manager = SmsManager.getDefault();
byte[] arrBytes = new byte[]{0,1,2,3,4,5,6,7,-100,-101,-105,-110};
manager.sendDataMessage(number, null, (short)0, arrBytes, null, null);
This is the code for receiving:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] smsMensajes = null;
if(bundle != null){
try{
Object[] pdus = (Object[]) bundle.get("pdus");
smsMensajes = new SmsMessage[pdus.length];
byte[] msjByte = new byte[100];
for(int i=0; i < smsMensajes.length; i++){
smsMensajes[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msjByte = smsMensajes[i].getUserData();
}
}
catch(Exception e){
Log.e("Recibiendo SMS", e.toString());
}
}
}
}
Thanks in advance :)
If you're fundamentally trying to send arbitrary bytes through a text-based interface, the safest way to do so is to use base-64. There's a Base64
class in Android which makes this very easy.
Admittedly this means the size is inflated a bit, so in 140 characters you'll only transmit 105 bytes, but at least it's safe and should work with any device and API.
See more on this question at Stackoverflow