About SMS Port for Android

I want to implement an application that receives SMS on a specific port.

Manifest code:

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

And the recipient class code is below.

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

        if(null != bundle)
        {
            String info = "Binary SMS from ";
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            byte[] data = null;

            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                info += msgs[i].getOriginatingAddress();                     
                info += "\n*****BINARY MESSAGE*****\n";

                data = msgs[i].getUserData();

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

            Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
        }
    }
}

I receive all SMS that are on this ( 8091) port or not. I want to receive only those messages that are specific to the port.

+3
source share
2 answers

For those who are still wondering why the application receives SMS data sent to other ports, and not just to port 8091. The problem is in the manifest code. I have seen many solutions that have been published, and most of them are actually erroneous.

The manifest should be:

<receiver
    android:name = ".SmsReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
        <data 
            android:scheme="sms"
            android:host="*"
            android:port="8095" />
        </intent-filter>
</receiver>

, "" , .

, " " ,
" URI, , ".
, , , SMS .

"host" * (), , SMS /

, - (:

+10

, "onReceive":

String dataStr = intent.getDataString();
if (dataStr.indexOf(":8888" ) == -1) {
    return;
}
+2

All Articles