Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

19.2 Sending Messages

It takes longer to explain how to send a message than it takes to write it out in code. Here is the short story:

public void sendText(String address, String text)
    throws IOException, InterruptedException {
  String cs = "sms://" + address + ":50000";
  MessageConnection mc = (MessageConnection)
  TextMessage tm = (TextMessage)

It's basically four lines, but the casts makes it look bulky. Here are the four steps:

  1. Obtain a MessageConnection from Connector. In the example, cs is the connection string, which I'll talk about soon.
  2. Get a Message from the MessageConnection by calling newMessage(). In this example, I asked for a text message.
  3. Fill up the message. This works differently for different message types. Here I called setPayloadText(), a method that is specific to TextMessage.
  4. Send the message by passing it to the MessageConnection's send() method.

The address in the connection string is the telephone number of the device to which you are sending the message. The port number is optional. The example above automatically appends a port number of 50,000.

If you leave off the port number, your message is an ordinary SMS message and will end up in an inbox at the destination. This is nice if you want an actual person to read the message.

If, instead, you are trying to send the message to an application running on the destination device, include a port number. Some port numbers are reserved. They are listed in the WMA specification. Any free port number should work, as long as the sender and recipient agree on that number.

In most cases, the push registry will field an incoming message and launch a MIDlet to respond. Go back to Chapter 6 if you can't remember the push registry. It's possible, but unlikely, that the receiving MIDlet will actually be running and listening for incoming messages. If no running MIDlet is listening for messages on the right port, and if no MIDlet is in the push registry for the right port, the message is likely to disappear entirely.

The people who named SMS were not kidding about the "short" part. Text messages can be, at most, 160 bytes, which allows for 160 characters in a single English message. Different message encodings will decrease the maximum message length. Furthermore, specifying a port number eats up 8 bytes.

Although individual message are small, WMA will actually split apart longer payloads into multiple messages, which will be reassembled at the receiving end. The specification requires implementations to be able to split long payloads into at least three messages. Keep in mind that many users must pay a small fee for each message, so even if it seems like you're sending one payload, your user might have to pay for more than one message.

Check out Table A-1 in the JSR 205 specification for a list of message lengths under different conditions.

  • + Share This
  • 🔖 Save To Your Account