Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Royster86
Posts: 102
Registered: ‎03-13-2012
My Device: 9360, P'9981, Z10
My Carrier: Vodafone, Virgin
Accepted Solution

Processing multipart SMS messages programatically

Hi all,

 

I'm trying to receive and concatenate multi part SMS messages with my app - however, I'm having some issues accessing the header segments from the SMS messages.

 

From what I can see, when I run byte [] data = datagram.getData();, it simply puts the text content of the message into a byte array.
 
Then when I run the following 2 lines provided below, I simply get the byte representation of the first and second characters of the message itself (no header information)
 
byte user_data_len = data[0];
byte user_data_header_len = data[1];
 
In example, if the text "Hello" is received, the two bytes obtained by the lines above would be 72 and 101 - when converted to ASCII this give us "He" - the same happens with a multipart message.
 
I'm not sure how this data is supposed to be related to the header information, as it just seems to be returning the content of the actual message itself.
 
To put this in context, here is a snippet of the source:
 
_dc = (DatagramConnection) Connector.open("sms://0");
for (;;) {
   if (_stop) {
   return;
   }
   Datagram d = _dc.newDatagram(_dc.getMaximumLength());
   _dc.receive(d);
 
   byte[] msgInfo = d.getData();
 
   byte user_data_len = msgInfo[0];
   byte user_data_header_len = msgInfo[1];
 
   //handle message here
 
}
 

 

Currently it looks as though the header information is inaccessible - whatever I try it seems to return just the content of the text message, with no headers or additional information.
 
Whats annoying is in the debug pane of Eclipse, I can see all the segments of data within the datagram:
 
The keys for the fields:
 
_properties
     _key
{sms-checksum,sms-ref-number,sms-total-segment,sms-segment-number}
 
The actual data:
 
This outlines the key names for the various components of the message.
For each message received, and for each key, there is a value associated
with it.

For example, a 2 part message consists of:

1st Message:
_properties
     _key
{sms-checksum,sms-ref-number,sms-total-segment,sms-segment-number}
     _value {-1697675679, 172, 2, 1}

2nd Message:
_properties
     _key
{sms-checksum,sms-ref-number,sms-total-segment,sms-segment-number}
     _value {-134365933, 172, 2, 2}
 
 
As you can see, the segment data is there, however I don't seem to be able to access it.
 
Many thanks for anyone that is able to help out!
 
 
Please use plain text.
Developer
myraddin
Posts: 405
Registered: ‎09-12-2008
My Device: 9000 9810

Re: Processing multipart SMS messages programatically

[ Edited ]
DatagramConnection datagramConnection = (DatagramConnection) Connector.open("sms://");

DatagramBase datagram = (DatagramBase) datagramConnection.newDatagram(datagramConnection.getMaximumLength()); datagramConnection.receive(datagram); // blocking int segCount = 1; int segNumber = 1; String messageID = null; try { segCount = ((Integer) datagram.getProperty("sms-total-segments")).intValue(); segNumber = ((Integer) datagram.getProperty("sms-segment-number")).intValue(); messageID = ((Integer) datagram.getProperty("sms-ref-number")).toString(); } catch (Exception e)
{ // this is not a concatenated SMS } String sender = datagram.getAddress(); String text = new String(datagram.getData());

 

Cheers



"Listen carefully, I shall say this only once"
Please use plain text.
Developer
Royster86
Posts: 102
Registered: ‎03-13-2012
My Device: 9360, P'9981, Z10
My Carrier: Vodafone, Virgin

Re: Processing multipart SMS messages programatically

myraddin - lifesaver - thanks very much - works a dream!!

Please use plain text.
Regular Contributor
walkline
Posts: 77
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Processing multipart SMS messages programatically

dear myraddin, i have been tried your code, it can be worked except the messageID, your code is below

 

messageID = ((Integer) datagram.getProperty("sms-ref-number")).toString();

 

I think that your meaning is "((String) datagram.getProperty.......", is it right?

 

However, i can not got messageID within Integer by using "sms-ref-number", please help to solve this problem, thank your very much!

Please use plain text.
Developer
myraddin
Posts: 405
Registered: ‎09-12-2008
My Device: 9000 9810

Re: Processing multipart SMS messages programatically

Hi,

It would be better to open a new ticket rather than continuing on a closed one :smileyhappy:

the sms-ref-number works only for multipart messages. You would use it in order to be able to assemble the complete message. Otherwise (single SMS), you don't need it.

I hope this answers your question.
If not, It could be a Device/OS version issue.

Cheers.


"Listen carefully, I shall say this only once"
Please use plain text.
Developer
myraddin
Posts: 405
Registered: ‎09-12-2008
My Device: 9000 9810

Re: Processing multipart SMS messages programatically

I am sorry, I forgot to address your other question.

"I think that your meaning is "((String) datagram.getProperty.......", is it right?"
No. The property is an Integer (otherwise you would get a classCastException). But I need it as a String :smileywink:

If you are new to Java, pls check Java related forums. Maybe something like this: http://www.coderanch.com/forums/f-33/java


"Listen carefully, I shall say this only once"
Please use plain text.
Regular Contributor
walkline
Posts: 77
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Processing multipart SMS messages programatically

Hi,

 

I tried to receive many multipart messages one by one, and the messageID were not null value, but zero.

 

I means that everytime I received the multipart messages, all of them would be the same one messageID, so I can not identify the different messages.

 

I also tried SMSPacketHeader.getID(), in the simulator, i got the ascending int value, but in device, i only got the max value of int type, 65535. I have no idear for this issue.:Sweatdrop:

Please use plain text.
Regular Contributor
walkline
Posts: 77
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Processing multipart SMS messages programatically

I got it~:Clap:

 

thanks myraddin~

 

your code really workable, thanks again, have a good day!:Walkman:

Please use plain text.