chinnadurai
chinnadurai

Reputation: 21

Android: Message in Data SMS is returning null

I have an Android application where i want to receive Data SMS on specific port. MY BroadcastReceiver's OnReceive method gets called when the SMS is reaching my phone. When i print the Intent, Log.i("SMS Receiver: Received intent", intent.toString()); i can see that the Datamessage has received on particular port. But, when i use msgs[i].getMessageBody().toString() it retuns null. The actual message is not available. I have cross checked the sender and it is fine. When i uninstall my application on device and send a data sms to my phone, the SMS goes to the inbox and on the phone the message is shown as "Unsupported content". Is it something to do with the format of the Message i am sending? Below are the manifest entries used for receiving Data SMS


-->

            <action android:name = "android.intent.action.DATA_SMS_RECEIVED"/>
               <data  android:scheme="sms"/>  
                <data android:host="localhost"/> 
                <data android:port="16000"/>

        </intent-filter> 
    </receiver>

</application>
<uses-sdk android:minSdkVersion="4" />

Any help would be highly appreciated.

Upvotes: 0

Views: 2378

Answers (1)

juniano
juniano

Reputation: 364

You probably used 'getMessageBody()' to get a message text. But that's for the text sms. For data sms, you will need to use 'getUserData() instead, to get a message body. The code should be something like this

Bundle bundle = intent.getExtras();        
SmsMessage[] msgs = null;
if (bundle != null)
{
    Object[] pdus = (Object[]) bundle.get("pdus");
    msgs = new SmsMessage[pdus.length];            
    for (int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
        byte[] userData = msgs[i].getUserData();
    }

}

Upvotes: 2

Related Questions