Android Question Sending SMS by PhoneSMS incl. capturing of 'Message Sent' and 'SMS Received' events etc.

beelze69

Active Member
Licensed User
Longtime User
Hi,

I have a doubt.

1) I am unable to send SMS by using PhoneSMS.

I have attached a sample source code.

In the attached code, I tried the solution given earlier of sending sms via 'Intent' (the relevant lines are commented in that code with a 'X ).

Though that code worked fine, I had to explicitly press the 'Send' button on the Phone to send the SMS.

The solution given in the forum to this was that we should use the PhoneSMS library (and also since my app is not to be published under Google Play so .. those restrictions do not apply)..

The error message shown is below.

It is to inform that the program when started does not ask for any enabling of permissions - it simply hangs...


2) Assuming this is possible, can someone post an example of how to capture the 'SMS Sent', 'SMS Received' events etc..

Please help.

Thanks..

The Error message :


Logger connected to: Xiaomi Redmi Note 7 Pro
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 352)
java.lang.SecurityException: Sending SMS message: uid 10239 does not have android.permission.SEND_SMS.
at android.os.Parcel.createException(Parcel.java:1953)
at android.os.Parcel.readException(Parcel.java:1921)
at android.os.Parcel.readException(Parcel.java:1871)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:871)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:342)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:325)
at anywheresoftware.b4a.phone.Phone$PhoneSms.Send2(Phone.java:644)
at anywheresoftware.b4a.phone.Phone$PhoneSms.Send(Phone.java:626)
at b4a.example.main._activity_create(main.java:352)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 

Attachments

  • b4ATestSMS.zip
    9.1 KB · Views: 393

DonManfred

Expert
Licensed User
Longtime User
Only the default SMS App on your Device is allowed to Send SMS.
I earlier Androids it was possible
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Only the default SMS App on your Device is allowed to Send SMS.
I earlier Androids it was possible
Hi Don,

Thanks ... So is it that I am better off with the 'Intent' way of sending the SMS (as opposed to the PhoneSMS thing)?

The result would be that the Message to be sent is 'readied' by the application and shown to the end-user and end-user has to 'explicitly press the SEND' button and then the Message goes ...

Please correct me if I am wrong ..

Secondly, since there were no replies to my Doubt 2 above, does it mean that PhoneSMS Library is not relevant today and should no longer be used by Android developers ?

Again.. please correct me if I am wrong..


Thanks
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Your error is shown below:
java.lang.SecurityException: Sending SMS message: uid 10239 does not have android.permission.SEND_SMS.

In the settings app on your phone, you need to find 'apps and notifications' (may be called something different based on your phone) > find your app > need to enable the SMS permission, then it will allow you to send SMS messages within the app.

I have used a Pixel4a (Running Android 11 - not-rooted) and got it to send the SMS and also receive the SMS in my B4A app.

I am not submitting it to Google Play so it's possible to be done. (If you upload the app to Google Play then it will get rejected by Google as SMS apps are not allowed on Google Play).

Here is how I got it to work:

B4X:
' Send SMS with this code. (I am sending it from the starter service)
Dim p As PhoneSms
    p.Send2(phonenumber,msg,False,False)
' replace phonenumber with the phone number you want to send the SMS to
' replace msg with the message you want to send

Receive SMS:
B4X:
Sub Service_Create
    Dim SI As SmsInterceptor
    SI.Initialize2("SI", 999)
End Sub

' Default SMS app on the phone still gets the SMS
Public Sub SI_MessageReceived (From As String, Body As String) As Boolean
    Log($"From: ${From}, Body: ${Body}"$)
End Sub
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Hi Aaronk,

Thanks a lot !

It worked !

I selected Permissions and 'allowed' SMS for my app - and it works - meaning there is no need to press the Send button now - it automatically sends the message upon code invokation using PhoneSMS (I did not use the Intent method).

One more request.

What does 999 in your SI.Initialize2("SI", 999) stand for ?

Please guide me.

2) From your reply I understand that I should be able to invoke a database from this SI_MessageReceived event and trap all messsages and store it in a database..

The intention here is to keep track of the 'responses' related to that 'Service Domain' for which the message is intended for. One could then request the end-user to prefix the messages with a specific Domain Name , for example, 'XYZPetServices:I have accepted your response' where XYZPetServices is the 'Service Domain'. From this it would be easier to sift through all messages related to that particular domain.

Please correct me if I am wrong.

Thanks.
 
Last edited:
Upvote 0
Top