Android Question PhoneSMS does not work

hatzisn

Expert
Licensed User
Longtime User
I am not able to send an sms with PhoneSMS library. My android version is 4.2.2.
My code runs from a service module. This is my code:

B4X:
Sub SendSMS()
  
    Dim pSMS As PhoneSms
    Dim cur As Cursor = Main.conn.ExecQuery("SELECT * FROM [00_AppOptions]")
    cur.Position = 0
    Dim sSMS As String
    ts.SetLanguage("en", "US")
    ts.Speak("Entered in Send SMS event.", False)
    sSMS = cur.GetString("SMSToSend")
    cur = Main.conn.ExecQuery("SELECT PhoneNumber FROM [01_NumbersForSMS]")
    cur.Position = 0
    Dim ii As Int
    Dim jj As Int
    Dim sNumber As String
    For ii = 0 To cur.RowCount - 1
        Dim sNumber2 As String
        cur.Position = ii
        sNumber = cur.GetString("PhoneNumber")
        sNumber2 = ""
        For jj = 0 To sNumber.Length - 1
            sNumber2 = sNumber2 & sNumber.CharAt(jj) & " "
        Next
        ts.Speak("Sending to " & sNumber2, False)
      
        pSMS.Send(sNumber, sSMS & " - " & sCoords)
    Next
    cur.Close
    Dim sp As SoundPool
    sp.Initialize(1)
    Dim iID As Int
    iID = sp.Load(File.DirAssets, "strings2.mp3")
    sp.Play(iID,1,1,100,1,2)
  
End Sub

It speaks all the TTS commands, it does not hung but it does not play either the mp3.
Are there any problems running the code in a service? The SMS never reaches each destination so I suppose I do not send it. The variable sCoords is defined in Process_Globals and gets its value from another sub. Any ideas?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
There should be no problem running code from a service.
Check the required permissions (Logs panel \ List permissions : android.permission.SEND_SMS)
Check the length of the SMS message (160 chars for ASCII, 70 chars for UTF-8)
Don't bomb the SMS : insert a delay or wait till the SmsDelivered event is fired to send the next one (PhoneEvents)
About the TTS : it should be initialized globally as it takes some times to be ready
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Strange but I found a solution. When there is even a short message in Greek (6 chars) it does not send the SMS. If the message is in English it is send. What could be the problem?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thanks Erel. It worked perfect.
 
Upvote 0
Top