Multipart sms

lymey

Active Member
Licensed User
Longtime User
I am working on a project that will receive large sms messages..i.e multipart messages.
Since the interceptor receives one part at a time as a separate SMS, is there a way of looking at the UDH so that I can piece the messages together or some way that the interceptor can concatenate the messages into a single message?

I know the sms app that comes with the phones do it for you, and I can get the whole message after the fact, but I need to get the multipart message on the way in and stop the default sms messenger getting the message.


Thanks!

:sign0104:
 

lymey

Active Member
Licensed User
Longtime User
Multipart sms...part two

Yes,
it appears that the B4A SMSinterceptor intercepts one message at a time in quick succession.

If you use B4A SMSmessages to retrieve the messages after they have been received then you discover that they have been pieced back and stored as a single message.

The problem is that if you use the B4A SMSInterceptor in a service that will stop the default phone app from receiving messages, it will only intercept the individual message segments. You then have to write a routine to catch the message segments using a home grown message header to piece them back together.

What I was hoping was that there was a similar way to intercept the multipart messages like there is to send them in B4A.

Anybody know how to do that?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
As far as I know working with GSM modules, the only way to correctly concatenate multipart messages, is to retrieve them in PDU binary mode, not text mode.
The sections of the message may arrive in the wrong order, delayed and with another message (that is not part of the chain) between.
So, SMSInterceptor should have a PDU binary mode so that you can fetch the various parts with the UDH header and then reassemble the whole SMS (a lot of job...)
I'm much also interested to what Erel says about this.
Marco
 
Upvote 0

lymey

Active Member
Licensed User
Longtime User
Multipart sms -three

Hi Erel,
Maybe I am missing something, but using that code the result is the same as the SMSinterceptor....you still get multiple messages. can you explain how I can tell it is a multipart message it in a little more detail?

Since there is no header information available, you have no easy way of piecing them back together reliably.

If you allow the phones default SMS app to intercept the messages, it pieces them back to a single message that you could then get with the SMSmessages.GetAll. But that doesn't solve the need to intercept the messages and put them back as a single message.
To do this there must be a way to look at the UDH of the SMS message to determine if it is part of a multipart message.

Thanks for your help, I appreciate it very much.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My answer was that I'm not sure what will return as it is not clear from createFromPdu documentation.

Based on this open source project: SmsReceiver.java - gtalksms - Receive notifications (calls, sms, power status...) and send commands (sms, geolocalization...) to your Android phone with an XMPP/GTalk account - Google Project Hosting

Code not tested:
B4X:
Sub ParseSmsIntent (In As Intent) As List
   Dim messages As List
   messages.Initialize
    If In.HasExtra("pdus") = False Then Return messages
    Dim pdus() As Object
    Dim r As Reflector
    pdus = In.GetExtra("pdus")
   Dim messagesMap As Map
   messagesMap.Initialize
    If pdus.Length > 0 Then
        For i = 0 To pdus.Length - 1
         Dim msg As Message
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
                Array As Object(pdus(i)), Array As String("[B"))
            msg.Body = r.RunMethod("getMessageBody")
            msg.Address = r.RunMethod("getOriginatingAddress")
         If messagesMap.ContainsKey(msg.Address) Then
            Dim existing As Message
            existing = messagesMap.Get(msg.Address)
            existing.Body = existing.Body & msg.Body
         Else
            messagesMap.Put(msg.Address, msg)
            messages.Add(msg)
         End If
        Next
    End If
    Return messages
End Sub
 
Upvote 0

lymey

Active Member
Licensed User
Longtime User
Multipart sms - part 4

hi Erel thanks for the code snippet,
it seems to catch the multipart messages ok. I haven't stress tested it yet.
BUT
I still have the same problem of intercepting a multipart message before the phone messaging app.

The code below will intercept the messages but at the same time as the default message app. If you use message interceptor it will intercept the messages but not recognize them as multipart messages. Naturally you cant use both, since the interceptor will intercept the message first.

So I am back to the question of how do I intercept multipart messages before the default phone app. Is there was some way of incorporating the intercept capabilities of SMSintercept, and the intent approach for multipart meassages?

B4X:
'Service module
Sub Process_Globals
    Type Message (Address As String, Body As String)
   Dim servicerunning As Boolean
    Dim notification1 As Notification
   'Dim theinterceptor As SmsInterceptor
End Sub
Sub Service_Create

If notification1.IsInitialized = False Then
     notification1.Initialize
     notification1.Icon = "notification_bw_x50"
     notification1.Vibrate = False
     notification1.Sound = False
    notification1.SetInfo("TEST-SMS", "Waiting For Messages", "")
    notification1.Notify(1)
End If
   
   'theinterceptor.Initialize2("smsint", 999)
   
    Log("MessageInterceptor Service initialized")
End Sub

Sub Service_Start(startingIntent As Intent)
    Service.StartForeground(1, notification1)
    servicerunning=True
    Log("MessageInterceptor Service Started")  
   If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        'Dim messages() As Message
      Dim messages As List
        messages = ParseSmsIntent(startingIntent)
        For i = 0 To messages.Size - 1
            Log(messages.get(i))
        Next
      notification1.Icon = "notification_color_x50"  
        notification1.Vibrate = True
        notification1.Sound = True
       notification1.SetInfo("New Messages", "Message received.", "")
      notification1.Notify(1)
   End If
End Sub

'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As List
    Dim messages_list As List
    messages_list.Initialize
    If In.HasExtra("pdus") = False Then Return messages_list
    Dim pdus() As Object
    Dim r As Reflector
    pdus = In.GetExtra("pdus")
    Dim messagesMap As Map
    messagesMap.Initialize
    If pdus.Length > 0 Then
        For i = 0 To pdus.Length - 1
            Dim msg As Message
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
                Array As Object(pdus(i)), Array As String("[B"))
            msg.Body = r.RunMethod("getMessageBody")
            msg.Address = r.RunMethod("getOriginatingAddress")
            If messagesMap.ContainsKey(msg.Address) Then
                Dim existing As Message
                existing = messagesMap.Get(msg.Address)
                existing.Body = existing.Body & msg.Body
            Else
                messagesMap.Put(msg.Address, msg)
                messages_list.Add(msg)
            End If
        Next
    End If
    Return messages_list
End Sub
Sub Service_Destroy
    Log("MessageInterceptor Service Destroyed")
      
   ' theinterceptor.StopListening()
   Service.StopForeground(1)
   servicerunning=False
   
End Sub
Sub smsInt_MessageReceived (From As String, Body As String) As Boolean
        notification1.Icon = "notification_color_x50"  
        notification1.Vibrate = True
        notification1.Sound = True
       notification1.SetInfo("New Messages", "Message received.", "")
      notification1.Notify(1)

Log("the interceptor does nothing now")
Return True
End Sub
 
Upvote 0
Top