Android Tutorial Max length of SMS message

My app will be sending SMS messages (using sms.send(phone, text) some of which may be longer than 160 chars. Currently, I have broken these down into multiple messages each of which is < 160 chars but this is a pain as I also have a receiving app which needs to put the messages back together again to process them

However, I did some experiments with normal text messaging on my Galaxy S2 and it seems that you can send and receive single messages longer than 160 chars. Whether or not Android OS sends these are multi-part messages and then rec-constitutes them I don't know. So I tried to have my app send an SMS longer than 160 chars and got a 'null pointer exception'. Is there any way round this limitation?

Thanks,
Seamus
 

sconlon

Active Member
Licensed User
Longtime User
Good old Hillebrand! But I was more interested to know how the ordinary text messaging within an Android phone (and also within iPhones) seem to send messages longer than 160 chars, a facility that would be nice if supported in B4A.
 

vb1992

Well-Known Member
Licensed User
Longtime User
"Larger content (concatenated SMS, multipart or segmented SMS, or "long SMS") can be sent using multiple messages, in which case each message will start with a user data header (UDH) containing segmentation information. Since UDH is part of the payload, the number of available characters per segment is lower: 153 for 7-bit encoding, 134 for 8-bit encoding and 67 for 16-bit encoding. The receiving handset is then responsible for reassembling the message and presenting it to the user as one long message. While the standard theoretically permits up to 255 segments,[36] 6 to 8 segment messages are the practical maximum, and long messages are often billed as equivalent to multiple SMS messages. Some providers have offered length-oriented pricing schemes for messages, however, the phenomenon is disappearing."


It's either going to be split up,
SmsManager.divideMessage(String text)
B4X:
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

or sent as a MMS
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code (requires Reflection library):
B4X:
Sub SendLargeSms(Destination As String, Message As String)
   Dim r As Reflector
   r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
   Dim parts As Object
   parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
   r.RunMethod4("sendMultipartTextMessage", _
      Array As Object(Destination, Null, parts, Null, Null), _
      Array As String("java.lang.String", "java.lang.String", _
         "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
End Sub

Add this line to the manifest editor:
B4X:
AddPermission(android.permission.SEND_SMS)
 

sconlon

Active Member
Licensed User
Longtime User
I tried your solution Erel and it works a treat. Many thanks.

Seamus
 

abay1968

Member
Licensed User
Longtime User
How to know whether the message was sent or not?

You can use this code (requires Reflection library):
B4X:
Sub SendLargeSms(Destination As String, Message As String)
   Dim r As Reflector
   r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
   Dim parts As Object
   parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
   r.RunMethod4("sendMultipartTextMessage", _
      Array As Object(Destination, Null, parts, Null, Null), _
      Array As String("java.lang.String", "java.lang.String", _
         "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
End Sub

Add this line to the manifest editor:
B4X:
AddPermission(android.permission.SEND_SMS)
 

KY Leng

Member
Licensed User
Longtime User
So, until now, there is no result concerning the concatenation of SMS with send notification status ?

You can use this code (requires Reflection library):
B4X:
Sub SendLargeSms(Destination As String, Message As String)
   Dim r As Reflector
   r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
   Dim parts As Object
   parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
   r.RunMethod4("sendMultipartTextMessage", _
      Array As Object(Destination, Null, parts, Null, Null), _
      Array As String("java.lang.String", "java.lang.String", _
         "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
End Sub

Add this line to the manifest editor:
B4X:
AddPermission(android.permission.SEND_SMS)

There is no confirmation event when using this method. You will need to use the standard Sms object instead.

But standard SMS object (PhoneSms and PhoneEvents) support only normal SMS. If we manually cut the long message to a multiple short messages, the receiver phone cannot combine those short messages automatically to be 1 long message.
 

KY Leng

Member
Licensed User
Longtime User
One more question, how to configure the message string so that

Dim SMSOk As PhoneSms
SMSOk.Send(SMSNum,SMSShortMessage)
can accept 140 character set in 1 sms.

Note that SMSShortMessage contain character number between [0 : 255].
I try to convert SMSShortMessage to byte with ASCII and convert back to string

Dim ByteData() As Byte
ByteData = SMSShortMessage.GetBytes("ASCII")
SMSLMessageANSI = BytesToString(ByteData, 0, ByteData.length, "ASCII")


But this one seem to limit the character set to [0:127]

and if I use UTF8 or UTF-8, they convert to 16-bit format, not 8-bit format => can send only 70 Characters [128:255]

Dim ByteData() As Byte
ByteData = SMSLMessage.GetBytes("UTF8")
SMSLMessageANSI = BytesToString(ByteData, 0, L, "UTF8")

So, could anyone tell me how to send the sms up to 140 special characters in 1 sms ?
 

janderkan

Well-Known Member
Licensed User
Longtime User
One more question, how to configure the message string so that

Dim SMSOk As PhoneSms
SMSOk.Send(SMSNum,SMSShortMessage)
can accept 140 character set in 1 sms.

Note that SMSShortMessage contain character number between [0 : 255].
I try to convert SMSShortMessage to byte with ASCII and convert back to string

Dim ByteData() As Byte
ByteData = SMSShortMessage.GetBytes("ASCII")
SMSLMessageANSI = BytesToString(ByteData, 0, ByteData.length, "ASCII")


But this one seem to limit the character set to [0:127]

and if I use UTF8 or UTF-8, they convert to 16-bit format, not 8-bit format => can send only 70 Characters [128:255]

Dim ByteData() As Byte
ByteData = SMSLMessage.GetBytes("UTF8")
SMSLMessageANSI = BytesToString(ByteData, 0, L, "UTF8")

So, could anyone tell me how to send the sms up to 140 special characters in 1 sms ?

To get the 8 bit characterset you should use "ISO 8859-1"
 
Top