Android Question SMS sending: complete procedure

Kibe

New Member
Hi. Am quite new to B4a. What is the complete procedure of sending sms? I have come across pieces of information till I got confused.
Thanks
 

Derek Johnson

Active Member
Licensed User
Longtime User
Add the Phone library then:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim PhoneNumber as String ="07700912345" 'UK format - un-issued number"
    Dim SMS As PhoneSms
    Dim Message As String = "Hi there"
    SMS.Send(PhoneNumber,Message)
End Sub
 
Upvote 0

Kibe

New Member
Thanks Derek.
I have tried using Kenyan format so far no success. Just copied your code the way it is. Am I supposed to do more that adding Phone library? There must be a way for the code to tell me whether the sms is sent and delivered.
Thanks
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
I presume that the device you are testing on has credit, and can send to the number that you are using in your code.

So you have a mobile number like "02547nnxxxxxx" where 02547 is the prefix for all Kenyan numbers when calling from inside Kenya.

There is a way to tell if the SMS has been sent from your phone, I'll post some more details on this later.
 
Upvote 0

Kibe

New Member
Thanks a lot Derek, I got it right now. I used +25472xxxxxxx
If you don't mind and at your own time, can I receive sms on my app just like I have been able to send?
Thanks.
 
Last edited:
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Here is some test code that will try to send an SMS and then detect if the SMS has been sent from your phone. It won't tell you though whether the network was able to deliver it though.

N.B Delivery notices do not work in my experience.

B4X:
#Region  Project Attributes
    #ApplicationLabel: SMSTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region


Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim SMS As PhoneSms
    Dim PE As PhoneEvents
    Dim PhoneID As PhoneId
    Private Button1 As Button
    Private Label1 As Label
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    EditText1.Color=Colors.White
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Sub Button1_Click
    Test
End Sub

Sub Test
    Log("Sending Message")
    PE.InitializeWithPhoneState("PE", PhoneID)
    Dim PhoneNumber As String ="07700900000" 'UK format - un-issued number!
    Dim Message As String = "Hi there at " & DateTime.Time(DateTime.Now)
    'SMS.Send(PhoneNumber,Message)
    Dim WantSentStatus As Boolean = True
    Dim WantDeliveryNotification As Boolean = True
    Try
        EditText1.Text=$"Sending Message to  ${PhoneNumber}
Message: ${Message}
"$
        SMS.Send2(PhoneNumber,Message,WantSentStatus,WantDeliveryNotification)
    Catch
        Log("Error when sending: " & LastException.Message)
        EditText1.Text=EditText1.Text &  LastException.Message & CRLF
    End Try   
End Sub

Sub PE_SmsDelivered (PhoneNumber As String, Intent As Intent)
    'Have never got a receipt from SMS using this!
    Dim Result As String
    Result="Receipt received from " & PhoneNumber
    Log(Result)
    EditText1.Text=EditText1.Text & Result & CRLF
End Sub

Sub PE_SmsSentStatus(Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent)
    Dim Result As String
    Result="SMS Sent Status:" & Success & " : " & ErrorMessage & " " & PhoneNumber
    Log(Result)
    EditText1.Text=EditText1.Text & Result & CRLF
End Sub

I've attached the zipped project.

You can go further and get copies of outgoing and incoming SMS messages using the SMSInterceptor object. This might be what you initially found confusing.

Derek
 

Attachments

  • SMSTest.zip
    8.4 KB · Views: 426
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Here's a more complete App that both sends and processes received SMS messages.

This is considerably more complex as it requires a background process to handle the receipt of SMS messages.

When an SMS message is received it adds a notification. Clicking on the notification (re)opens the App.

Derek
 

Attachments

  • SMSTest2.zip
    9.8 KB · Views: 403
Upvote 0
Top