Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive data sms in two different ports

I have two application with two different data sms broadcast and I want to receive data sms in both of them. For that I have created broadcast in each app

Receiver one in app 1:

 <receiver android:name=".SMSReceiver" >
            <intent-filter android:priority="10" >
                <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
                <data android:scheme="sms" />
                <data android:port="5555" />
            </intent-filter>
  </receiver>

Receiver two in app two:

  <receiver android:name=".SMSReceiver1" >
            <intent-filter android:priority="10" >
                <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
                <data android:scheme="sms" />
                <data android:port="8901" />
            </intent-filter>
   </receiver>

Now the scenario is this, sms received only in first installed app, I have tried with same ports, class name and also with different. But problem not solved, How I can solve it?? Here is my broadcast receiver class

public class SMSReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();

        SmsMessage[] messages = null;
        String strMessage = "";
        byte[] data = null;

        if (myBundle != null) {

            Object[] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                phoneNo = messages[i].getOriginatingAddress();
                try {
                    data = messages[i].getUserData();

                } catch (Exception e) {

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

            }

            message = strMessage;


            System.out.println("message received: " + message);

    }
}

and my code for sending message

SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendDataMessage(myNumber, null,(short)8901, sms.getBytes(), null, null);
like image 885
Talha Avatar asked Nov 03 '22 07:11

Talha


2 Answers

to receive sms from a different port, set this receiver in your AndroidManifest.xml

<receiver android:name=".SMSBcastReceiver">
    <intent-filter android:priority="10"> 
    <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
<data android:scheme="sms" /> 
        <data android:port="13013" /> 
</intent-filter>
</receiver>

then set this in your SMSBcastReceiver class

public void onReceive(final Context context, Intent intent) {

Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];

for (int n = 0; n < messages.length; n++) 
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}

smsOrigin = smsMessage[0].getDisplayOriginatingAddress();
smsData = smsMessage[0].getUserData();

for(int index=0; index<smsData.length; ++index)
{
smsBody += Character.toString((char)smsData[index]);
}
}
like image 200
GOLDEE Avatar answered Nov 09 '22 12:11

GOLDEE


Couple of things to double check:

a) Are you sure you have added android.permission.RECEIVE_SMS permission in both applications?

b) Can you remove this piece from both manifests to make sure that intent filtering doesn't mess up?

<data android:scheme="sms" />
<data android:port="5555" />

c) If b) will work, you may be interested to do filtering in your Receiver code.

like image 33
Victor Ronin Avatar answered Nov 09 '22 12:11

Victor Ronin