Reputation: 31
I want implement an app which 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 receiver class code 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 am getting all SMS which are on this (8091
) port or not. I want to receive only those messages which are port specific.
Upvotes: 3
Views: 4596
Reputation: 156
For those who are still wondering why the app is receiving data SMS directed to other ports and not just to port 8091. The problem lies in the Manifest code. I've seen many solutions that were posted and most of them are actually wrong.
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>
The scheme, host and port attributes must be defined in only one "data" element of the intent-filter, not in separate "data" elements.
Also not that from this link, under "data test" section it states that
"The host and port together constitute the URI authority; if a host is not specified, the port is ignored."
So keep in mind that you need to specify the host if you want the app to receive the data SMS for only a specific port.
The "host" element is * (asterisk) is to specify that it accepts data SMS from all hosts / phone numbers
Hope this helps someone (:
Upvotes: 11
Reputation: 21
I had similar problem, just add following check condition at begin of your 'onReceive' code:
String dataStr = intent.getDataString();
if (dataStr.indexOf(":8888" ) == -1) {
return;
}
Upvotes: 2