Edwin Ho
Edwin Ho

Reputation: 91

How to send non-printing characters via SMS

Anyone know how to send non-printing characters via SMS in Android?

I tried the following code but it does not work...The recipient will not receive the correct string.

String msg = "Testing special char" +(char) 3;
sendSMS(num,msg);//defined method

Or is there any other way to insert some kind of tags into a SMS, so that the recipient can perform some actions accordingly?

Upvotes: 4

Views: 712

Answers (2)

berezovskyi
berezovskyi

Reputation: 3491

As there is an Android tag on the question, here is what I found while researching the topic (code from codetheory.in).

Send:

// Get the default instance of SmsManager
SmsManager smsManager = SmsManager.getDefault();

String phoneNumber = "9999999999";
byte[] smsBody = "Let me know if you get this SMS".getBytes();
short port = 6734;

// Send a text based SMS
smsManager.sendDataMessage(phoneNumber, null, port, smsBody, null, null);

Receive:

public class SmsReceiver extends BroadcastReceiver {
    private String TAG = SmsReceiver.class.getSimpleName();

    public SmsReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // Get the data (SMS data) bound to intent
        Bundle bundle = intent.getExtras();

        SmsMessage[] msgs = null;

        String str = "";

        if (bundle != null){
            // Retrieve the Binary SMS data
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            // For every SMS message received (although multipart is not supported with binary)
            for (int i=0; i<msgs.length; i++) {
                byte[] data = null;

                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                str += "Binary SMS from " + msgs[i].getOriginatingAddress() + " :";

                str += "\nBINARY MESSAGE: ";

                // Return the User Data section minus the
                // User Data Header (UDH) (if there is any UDH at all)
                data = msgs[i].getUserData();

                // Generally you can do away with this for loop
                // You'll just need the next for loop
                for (int index=0; index < data.length; index++) {
                    str += Byte.toString(data[index]);
                }

                str += "\nTEXT MESSAGE (FROM BINARY): ";

                for (int index=0; index < data.length; index++) {
                    str += Character.toString((char) data[index]);
                }

                str += "\n";
            }

            // Dump the entire message
            // Toast.makeText(context, str, Toast.LENGTH_LONG).show();
            Log.d(TAG, str);
        }
    }
}

Upvotes: 0

Kostadin
Kostadin

Reputation: 2559

By default you send sms text messages in ascii format. Try to send binary SMS.

Upvotes: 2

Related Questions