Search code examples
androidbinarysmsintentfilter

Binary SMS receiving on Android: dynamic receiver registration


I can send and receive data SMS on Android using the following code snippets:

SmsManager manager = SmsManager.getDefault();
manager.sendDataMessage(phonenumber, null, (short) PORT, data,piSend, piDelivered);

where PORT=8091 for the sender. And for the receiver, a broadcast intent is defined in the manifest file as follows:

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

then received in the DataSMSReceiver class.

However, I need to register the broadcast receiver dynamically as I do for text SMS e.g.:

protected static final String SMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED";
getApplicationContext().registerReceiver(mReceiver, new IntentFilter(SMS_RECEIVED));

How can I specify the port in the IntentFilter to register the receiver dynamically?

Thanks in advance.


Solution

  • I believe you need to use addDataAuthority(String, String). For example:

    IntentFilter filter = new IntentFilter(SMS_RECIEVED);
    filter.addDataAuthority("*", "8091");
    filter.addDataScheme("sms");
    
    //use the filter, etc...