Syrko
Syrko

Reputation: 61

Android: Attempting to send an SMS via SmsManager redirects the message to the default app with no address

As per the tile, I use the following code:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(sms_receiver, null, sms_content, null, null);

No errors and no exceptions. While the above seems to work fine in android studio's emulator that I use, when I install my app on my device, again no errors or crashes, but the message goes to my default SMS app as a draft with no recipient.

After searching I, was lead to believe that the fact my mobile phone is dual sim (although I have only one inserted in my device) is the problem and can be addressed by using the "scAddress" argument.

I'm still not sure if the above is right and I couldn't find a way to get the scAddress programmatically.

If it is at all relevant, the model I have is a Xiaomi Redmi Note 6 Pro and the API of the android app:

 minSdkVersion 25
 targetSdkVersion 29

Upvotes: 0

Views: 1065

Answers (2)

Syrko
Syrko

Reputation: 61

The problem was indeed that the

SmsManager smsManager = SmsManager.getDefault();

wasn't getting the active sim slot. I used the snippet found in this answer: Android: How to send sms with particular SIM in dual sim mobiles?

Now the app needs READ_PHONE_STATE permission, which I would prefer wasn't the case, but it works as expected (i.e the SMS is being sent by my app).

Upvotes: 0

Greg
Greg

Reputation: 63

I assume you've tried setting the service center address in the second argument of smsManager.sendTextMessage?

https://developer.android.com/reference/android/telephony/gsm/SmsManager#sendDataMessage(java.lang.String,%20java.lang.String,%20short,%20byte[],%20android.app.PendingIntent,%20android.app.PendingIntent)

Looks like it should be a phone number: Look here for more info:

https://www.developershome.com/sms/cscaCommand.asp

Upvotes: 0

Related Questions