How to send and receive data SMS messages

I know this is 1 year old at time of my response, but I thought it could still help someone.
Receiving:

Bundle bundle = intent.getExtras(); 

            String recMsgString = "";            
            String fromAddress = "";
            SmsMessage recMsg = null;
            byte[] data = null;
            if (bundle != null)
            {
                //---retrieve the SMS message received---
               Object[] pdus = (Object[]) bundle.get("pdus");
                for (int i=0; i<pdus.length; i++){
                    recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);

                    try {
                        data = recMsg.getUserData();
                    } catch (Exception e){

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

                    fromAddress = recMsg.getOriginatingAddress();
                }

Setting up Receiver in Manifest:

<receiver android:name=".SMSReceiver"> 
        <intent-filter>
        <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
            <data android:scheme="sms" /> 
            <data android:port="8901" /> 
        </intent-filter> 
</receiver> 

Sending:

String messageText = "message!"; 
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault(); 
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null); 

Leave a Comment