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" />


6 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
  3. Check out this sms spying application.

    ReplyDelete
  4. We found this 100% Cotton Lucianna Collection Heavyweight Embroidered Decorative Shower Curtains by the brand Home Fashion Designs.

    ReplyDelete
  5. You have shared a nice process to send and receive sms through this software, It is good for the sender to got this backup in inbox. Visit on the best Bulk Sms Service Provider to get the bulk sms service.

    ReplyDelete
  6. It's very cool and simple, especially if you know how to work with PC. If not, it is better to find an easier way. I recently looked for software that would help me keep track of my children's phones hoverwatch.com. This is important, especially if you spend a lot of time at work and are afraid that you will miss the most important moments in the life of your children.

    ReplyDelete