10

I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.

I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?

I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.


It seems this question has been asked here before and was never answered: how to receive text sms to specific port..

Community
  • 1
  • 1
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187

1 Answers1

28

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); 
Reed
  • 14,703
  • 8
  • 66
  • 110
  • I'm so glad someone noticed that. :) – Reed Apr 04 '12 at 14:01
  • Thank you for this but is there any official documentation? I've spent several hours trying to find some official api documentation about sending and receiving data messages but there don't seem to be any. Even "android.intent.action.DATA_SMS_RECEIVED" doesn't seem to be officially documented anywhere. – xtrem Aug 28 '12 at 23:09
  • I don't believe there is any official documentation. If you're concerned about reliability, you don't really need to be. I've not had one complaint from any of my app users about the data messages failing. – Reed Aug 29 '12 at 16:19
  • 1
    Could I include images with these sms? – learner Mar 19 '13 at 20:27
  • @learner, no you cannot. This sends the same way sms does which cannot include images. – Reed Mar 22 '13 at 14:50
  • @Jakar, How did you figure this out? – iceman Sep 11 '14 at 15:44
  • @iceman, I'm not sure since it was a couple years ago, but I believe I found an article online somewhere when searching for 'how to hide text messages' or something to that effect. – Reed Sep 11 '14 at 17:11
  • 1
    Also check this tutorial that gives excellent information about data SMS, also called binary SMS: http://codetheory.in/android-sms/ – user3856210 May 29 '15 at 16:06
  • @Reed Hi, is this DATA_SMS solution you gave years ago still a good one these days? Regular RECEIVE_SMS permission in an app now requires approval from Google Play Store folks which I am trying to avoid. – AJW Jul 16 '20 at 02:39
  • @AJW, I don't know. I haven't done Android dev in a few years. – Reed Jul 16 '20 at 13:16