Android Question SMS delivery report

pxpto

Member
Licensed User
Longtime User
Hi,

I 'm working on an app that sends SMS and receives the delivery reports. My problem is that I need to match the delivery report with the sent SMS. Matching the Sent/Report by phone number is not an option because I can send several SMS to the same number.

This is my test code:

B4X:
Sub Globals
    Dim ThisPhoneSms As PhoneSms   
    Dim PhoneId As PhoneId
    Dim PE As PhoneEvents   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("deliveryreporttest.bal")

    PE.InitializeWithPhoneState("PE", PhoneId)
   
    ThisPhoneSms.Send2("+xxxxxxxxxxxx", "Test", True, True)
End Sub


Sub PE_SmsSentStatus (Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent)
    Log("--- PE_SmsSentStatus ---")
   
    Log("Success:" & Success)
    Log("ErrorMessage:" & ErrorMessage)
    Log("PhoneNumber:" & PhoneNumber)
   
    Log("Sent Intent: " & Intent)
    Log("Sent Intent GetData: " & Intent.GetData)
    Log("Sent Intent Action: " & Intent.Action)
    Log("Sent Intent Flags: " & Intent.Flags)
    Log("Sent Intent ExtrasToString: " & Intent.ExtrasToString)
End Sub


Sub PE_SmsDelivered (PhoneNumber As String, Intent As Intent)
    Log("--- PE_SmsDelivered ---")
   
    Log("PhoneNumber:" & PhoneNumber)
   
    Log("Delivered Intent: " & Intent)
    Log("Delivered Intent GetData: " & Intent.GetData)
    Log("Delivered Intent Action: " & Intent.Action)
    Log("Delivered Intent Flags: " & Intent.Flags)
    Log("Delivered Intent ExtrasToString: " & Intent.ExtrasToString)
    Log("Delivered Intent GetExtra(PDU): " & Intent.GetExtra("pdu"))
   
    Dim bc As ByteConverter
    Log("Decoded PDU: " & bc.HexFromBytes(Intent.GetExtra("pdu")))
End Sub

The log I get:


I've hidden the destination number for privacy issues.

So, I see in the log that the SMS was sent and delivered.

When the SMS is delivered, I can see its PDU. Using this website to decode it, I can see that there is a value called "Reference" (Value "137") that is incremented automatically on each SMS I send.

(great article on how to decude PDU messages here)

According to the GSM specification, this "Reference" value in the delivery report is called TP-MR. It is the same as the TP-MR of the sent SMS for which the delivery report has been received.

This is exactly what I need: I need to know the reference number (TP-MR) of the sent SMS so that when I receive the delivery report I can match it with its reference number (TP-MR).

Does anyone know how to do this?

Regards,
Jorge
 

pxpto

Member
Licensed User
Longtime User
Thank you for your suggestion Earl. That was exactly what I needed.

I'm using B4A 3.50, so I coudn't use the inline Java code. I had to make a library with the "Send3" java code and I can get it to work.. sort of..

Here's my test code:

B4X:
Sub Globals
    Dim ThisPhoneSms As SendSmsWithExtraData
    Dim PhoneId As PhoneId
    Dim PE As PhoneEvents
End Sub

Sub Activity_Create(FirstTime As Boolean)
    PE.InitializeWithPhoneState("PE", PhoneId)
   
    Dim id As String
    Dim Extra As Map
   
    id = "123"
    Log("Sending SMS with id: " & id)
    Extra.Initialize
    Extra.Put("id", id)
    ThisPhoneSms.Send3("+000000000000", "Test", Extra, True, True)
   
    id = "456"
    Log("Sending SMS with id: " & id)
    Extra.Initialize
    Extra.Put("id", id)
    ThisPhoneSms.Send3("+000000000000", "Test", Extra, True, True)
End Sub

Sub PE_SmsSentStatus (Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent)
    Log("PE_SmsSentStatus:")
    Log("- PhoneNumber: " & PhoneNumber)
    Log("- Success: " & Success)
    Log("- id: " & Intent.GetExtra("id"))
End Sub

Sub PE_SmsDelivered (PhoneNumber As String, Intent As Intent)
    Log("PE_SmsDelivered:")
    Log("- PhoneNumber: " & PhoneNumber)
    Log("- id: " & Intent.GetExtra("id"))
End Sub

The result log is:


So, I send 2 SMS's to the same GSM number. The first one with "id" 123 and the second one with "id" 456. And then I receive the SentStatus for both.. and the SmsDelivered for both. And all events have the correct "id". Perfect!

The problem is that I can only run this code 1 time. If I run this code a second time I get this error:


The only way to make it work again is to delete the "Files" and "Objects" folders and leave just the .b4a file.

What am I doing wrong?

Library attached.
 

Attachments

  • SendSmsWithExtraData.zip
    3.8 KB · Views: 296
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…