Wednesday, 19 September 2012

Use SMS to send and receive raw data within your App

Objective


Use SMS to send and receive raw data within your App.

Issue


When you want send information with the function SMSManager#sendTextMessage, the sent SMS is stored in the sent messages list, then it triggers a notification when the recipient received this message and the system store it in the inbox in plain text.
With this method, there is not security and it's annoying for the user, because he has to clean up its inbox.

Solution

Steps
1. Send your message on a specific port with the SMSManager#sendDataMessage function
2. Register a BroadcastReceiver on the same port
3. Add the required permissions in your manifest
Details
1. Send your message on a specific port with the SMSManager#sendDataMessage function
   

    public void sendMessage(final byte[] message) {
        //define the phone number
        final String port = "90";
        final String phoneNumber = "5146792345"
        //intent broadcasted when the SMS is sent
        final PendingIntent sendIntent = PendingIntent.getBroadcast(
                this.context, 0, new Intent(0), 0);
        //intent broadcasted when the SMS is received
        final PendingIntent delivery = PendingIntent.getBroadcast(
                this.context, 0, new Intent(0), 0);
        //send data
        final SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendDataMessage(phoneNumber, "", port,
                message, sendIntent, delivery);
    }


2. Register a BroadcastReceiver on the same port
   

        BroadcastReceiver receivedBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final Bundle bundle = intent.getExtras();
                SmsMessage[] msgs = null;

                if (null != bundle) {
                    final Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    byte[] data = null;                    
                    //read data
                    for (int i = 0; i < msgs.length; i++) {
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        data = msgs[i].getUserData();
                    }
                    if (data != null) {
                       //use data
                    }
                }
            }
        };
        //register the receiver
        final String port = "90";
        final IntentFilter intentFilter = new IntentFilter(
                "android.intent.action.DATA_SMS_RECEIVED");
        intentFilter.addDataScheme("sms");
        intentFilter.addDataAuthority("*", port);
        this.context.registerReceiver(this.receivedBroadcastReceiver,
                intentFilter);


3. Add the required permissions in your manifest.
   

    <uses -permission="-permission" android:name="android.permission.SEND_SMS" />
    <uses -permission="-permission" android:name="android.permission.RECEIVE_SMS" />


2 comments:

  1. Could someone using sms tracking application receive the same data? Look at this article to get where I am going

    ReplyDelete
  2. Daredevil and Bullseye are going to have a face off for sure in Daredevil season 3 as the antagonist is expected to appear in the upcoming installment. Daredevil Season 3 Trailer

    ReplyDelete