Android Tutorial How to send/receive Data SMS

Erel

B4X founder
Staff member
Licensed User
Longtime User
Send data message:
B4X:
Dim sm As JavaObject
sm = sm.InitializeStatic("android.telephony.SmsManager").RunMethod("getDefault", null)
Dim port As Short = 90
Dim Destination As String = "123456778"
Dim Data() As Byte = "hello".GetBytes("UTF8")
sm.RunMethod("sendDataMessage", Array(Destination, "", port, data, null, null))

To receive the message you need to follow this tutorial: http://www.b4x.com/android/forum/th...sms-messages-in-the-background.20103/#content

You will need to listen to add an intent filter similar to the one posted here: http://stackoverflow.com/questions/14209889/how-to-receive-data-sms-in-two-different-ports

Call r.RunMethod("getUserData") to get the data from the message.
 

mitobobo

Member
Licensed User
Longtime User
Send data message:
B4X:
Dim sm As JavaObject
sm = sm.InitializeStatic("android.telephony.SmsManager").RunMethod("getDefault", null)
Dim port As Short = 90
Dim Destination As String = "123456778"
Dim Data() As Byte = "hello".GetBytes("UTF8")
sm.RunMethod("sendDataMessage", Array(Destination, "", port, data, null, null))

To receive the message you need to follow this tutorial: http://www.b4x.com/android/forum/th...sms-messages-in-the-background.20103/#content

You will need to listen to add an intent filter similar to the one posted here: http://stackoverflow.com/questions/14209889/how-to-receive-data-sms-in-two-different-ports

Call r.RunMethod("getUserData") to get the data from the message.

Thank you very much for your answer, Erel! You're always very kind :)
Your code sends the DATA SMS (when I check on my provider's website, I can see it). I followed your tutorial (which I already knew) and replaced
B4X:
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
with ".... android.provider.Telephony.DATA_SMS_RECEIVED".
Without adding any filter for the moment, I should be able to catch the DATA SMS already. Though nothing happens.... :( Any idea why?
In the Manifest, I also added the permission to receive SMS.

Thank you so much again for your time.
 

Attachments

  • testsmsdata.zip
    7.2 KB · Views: 1,489

mitobobo

Member
Licensed User
Longtime User
Thank you so much, Erel! Luckily you exist.... :)
Your code works and I'm able to receive the data sms now.

When I want to get the DATA SMS recipient and body, I use this code:

B4X:
If StartingIntent.HasExtra("pdus") == False Then
  Log("No pdus")
Else
  Dim pdus() As Object
  Dim R As Reflector
  pdus = StartingIntent.GetExtra("pdus")
  If pdus.Length > 0 Then
      For i = 0 To pdus.Length - 1
        R.Target = R.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
        Body = R.RunMethod("getMessageBody")
        Log(Body)
        From = R.RunMethod("getOriginatingAddress")
        Log(From)
      Next
  End If
End If

It works fine.

Thank you again!
 

mitobobo

Member
Licensed User
Longtime User
Do you know what is the maximum size of these messages?
It should be 266 characters but will test it later tonight :) They're really useful since these kind of SMS are not detected by messaging apps even in KitKat.
 
Last edited:

haddad

Member
Licensed User
Longtime User
hi
i want to make a service listner for sms , that it's can let me when i get sms , its will be sent by e-mail to my box-email

tks
 

mitobobo

Member
Licensed User
Longtime User
It doesn't work anymore :-( I still receive the data message, though body of the message is "null" now. There still is the sender number but it seems that it receives an empty message. Maybe do you have any idea why?....
 

mitobobo

Member
Licensed User
Longtime User
This code:
B4X:
Dim sm As JavaObject
sm = sm.InitializeStatic("android.telephony.SmsManager").RunMethod("getDefault", null)
Dim port As Short = 90
Dim Destination As String = "123456778"
Dim Data() As Byte = "hello".GetBytes("UTF8")
sm.RunMethod("sendDataMessage", Array(Destination, "", port, data, null, null))

Thank you very much in advance for your patience, Erel.
 

mitobobo

Member
Licensed User
Longtime User
I simply love you:) It works perfectly! Thank you SO SO SO much for all your help! You're always very kind and patient.
 

androidsoftcoder

Member
Licensed User
Longtime User
is it possible to send data sms via second sim in 2sim phones?
for example I have 2sim phone and my app got data sms via second sim.
I want to discover via which sim sms was arrived and send reply also from the same (or other) sim.
 
Top