Android Question incoming sms catcher

Pablo Torres

Active Member
Licensed User
Longtime User
Hi everyone, I'm trying to develop an app (or a sevice, I don't know) that "catch" any incoming sms message, compare the sending phone number to a specific number and, if numver matches, then print the sms body using a bluetooth printer.
I do have ready the printing part, but I still can't catch the incoming sms.
I read all the documentation about, tried my best to solve it but didn't succeed at all.
Can anyone help me? Please
A portion of code or a little example will be great.
Thanks
 

Pablo Torres

Active Member
Licensed User
Longtime User
Yes I did, like I told you last time when you suggested exactly the same to me. I appreciatte the answer, but I need a portion of code that works for me, not this.
 
Upvote 0

Pablo Torres

Active Member
Licensed User
Longtime User
Here it goes the code of the Main Activity

B4X:
#Region  Project Attributes
    #ApplicationLabel: CMIB
    #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
  Dim SI As SmsInterceptor
End Sub

Sub Globals
    Dim Toggla As Toggle
    Dim cmp20 As Serial
    Dim printer As TextWriter
    Dim DirMac As String
    Dim PrintBuffer As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Toggla.TurnBluetoothOn
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SI_MessageReceived (From As String, Body As String) As Boolean
  Imprimir(Body, From)
  ToastMessageShow("De "& From & Chr(13) & Chr(10) & Body,True)
  Return True
End Sub

Sub Imprimir(strBody As String, strNumero As String)

PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " Numero: "& strNumero & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " Mensaje: "& strNumero & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
PrintBuffer=PrintBuffer &  " " & Chr(13)&Chr(10)
   
StartPrinter

End Sub

Sub StartPrinter
    Dim PairedDevices As Map

    PairedDevices.Initialize

    Try
        PairedDevices = cmp20.GetPairedDevices
    Catch
        Msgbox("Imposible conectar Bluetooth","Error de Impresión")
        printer.Close
        cmp20.Disconnect
    End Try

    If PairedDevices.Size = 0 Then
        Return
    Else
        cmp20.Connect("98:d3:31:b2:1d:ff")
    End If
   
End Sub

And here it goes the code of the service called s1

B4X:
#Region  Service Attributes
    #StartAtBoot: true
#End Region

'Service module
Sub Process_Globals
  Type Message (Address As String, Body As String)
End Sub

Sub Service_Create
Dim SI As SmsInterceptor
  SI.Initialize2("SI", 999)
End Sub

Sub Service_Start(startingIntent As Intent)
  If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
      Dim messages() As Message
      messages = ParseSmsIntent(startingIntent)
      For i = 0 To messages.Length - 1
        Log(messages(i))
      Next
  End If
End Sub

'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As Message()
  Dim messages() As Message
  If In.HasExtra("pdus") = False Then Return messages
  Dim pdus() As Object
  Dim r As Reflector
  pdus = In.GetExtra("pdus")
  If pdus.Length > 0 Then
      Dim messages(pdus.Length) As Message
      For i = 0 To pdus.Length - 1
        r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
        messages(i).Body = r.RunMethod("getMessageBody")
        messages(i).Address = r.RunMethod("getOriginatingAddress")
      Next
  End If
  Return messages
End Sub

It does compile, but it's not working
what am I missing?
 
Upvote 0
Top