Java Reference
In-Depth Information
Tip Remember that if you are using a server mode MessageConnection to send messages, you must
set the recipient address explicitly on each new message that you create— before you call send() . If you are
reusing an incoming message for replies, however, this is not necessary. You just need to set a new payload
(the reply). The address of an incoming message is already filled in with the sender's address.
Sending Text SMS Messages
Sending a text message is similar to sending binary messages. First, create an empty message
using MessageConnection 's newMessage() method, specifying MessageConnection.TEXT_MESSAGE
as its type.
Next, set the payload with the text string that you want to send:
public void setPayloadText(String data);
Finally, use the send() method of MessageConnection to send the message. Here is a code
fragment that you may use to open a connection and send an SMS text message:
MessageConnection conn =
(MessageConnection) Connector.open("sms://5550001:1234");
TextMessage txtmessage = (TextMessage) conn.newMessage(
MessageConnection.TEXT_MESSAGE);
txtmessage.setPayloadText(msgString);
conn.send(txtmessage);
Just like binary messages, you can also send large text messages that span multiple SMS
messages. The WMA API will handle segmentation at the sending end and reassembly at the
receiving end. However, some character encoding (namely UCS-2) limits the length of each
SMS text message to only 72 characters (66 available due to the port information used by WMA).
To accommodate this, the maximum sized text message that you can send safely is 189 characters
(3 SMS segments). See JSR 205 for details of payload length calculation if you are interested.
Receiving SMS Messages
To receive messages using a MessageConnection , you have two choices:
Use a blocking receive() method.
Implement a listener callback to be notified when a message arrives.
When using the blocking receive() method, you typically will be managing your own
threads. Using a listener allows you to code the logic in a callback method, without creating
and managing additional threads. You can choose either approach.
 
Search WWH ::




Custom Search