Revision as of 09:47, 9 August 2012 by skalogir (Talk | contribs)

How to send a message to a given port as SMS with Java ME

From Nokia Developer Wiki
Jump to: navigation, search

This code example demonstrates how to use the Wireless Messaging API in order to send a text message to a specific port. Specifying the port number is of high importance when you use this application in combination with another application that listens to the same port for incoming messages. The device's AMS (Application Management Software), can also assign a port to a given application and launch that application via PushRegistry when a message arrives.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 303
Nokia 311
PureView 808
Device(s): Series 40, Symbian
Keywords: javax.wireless.messaging.MessageConnection, javax.wireless.messaging.TextMessage
Created: skalogir (23 Jul 2012)
Last edited: skalogir (09 Aug 2012)



High-level LCDUI Components are used for creating the application's UI, which consists of a numeric field for typing the recipient's number, a message body, the Send button and an Exit button.

The User Interface

Both TextFields are empty by default. The TextField at the top of the form becomes of type PHONENUMBER and can take up to 20 digits, while the TextField beneath it, becomes of type PLAIN and takes up to 160 characters, which is the character limit for an SMS transmission without splitting it up to more than one piece. In the constructor, we instantiate the MessageConnection object and bind it to port 6553. Also a TextMessage instance is created of type TEXT_MESSAGE, since this example will be used for sending a message containing characters in the form of a text. If the port cannot be opened, an error message will be appended to the form.

mainForm = new Form("SMS Example");
smsNumber = new TextField("Phone number", null, 20, TextField.PHONENUMBER);
smsText = new TextField("Text", null, 160, TextField.PLAIN);
sendCommand = new Command("Send", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 0);
// Open the connection
connection = (MessageConnection)Connector.open("sms://:6553");
message = (TextMessage)connection.newMessage(MessageConnection.TEXT_MESSAGE);
catch (IOException ex) {
mainForm.append("error:" + ex.getMessage() + "\n");

Sending the text message

The method handleSendCommand is called via the CommandListener when the user selects the Send action. The TextMessage instance is being given an address in the form sms:// [recipient number] : [port number]. The port number is hard coded in this example to 6553. A payload is added to the message by calling the setPayloadText method and passing as argument the content of the second TextField.

private void handleSendCommand() {
//Set the destination address
String number = "sms://" + smsNumber.getString() + ":6553";
// Obtain the specified text and set it as the payload
String text = smsText.getString();

After a payload and an address is set to the message, the sending operation takes place inside a thread:

// Send the message on its own thread of execution
Thread smsThread = new Thread() {
public void run() {
try {
mainForm.append("Message sent!\n");
catch (InterruptedIOException ex) {
mainForm.append("Interrupted Exception!\n");
catch (IOException ex) {
catch (IllegalArgumentException ex) {
mainForm.append("Illegal Argument!\n");
catch (SecurityException ex) {

In order to send the message, as seen from above, we only need to call the send method from the connection instance and pass as argument the TextMessage instance that now contains both a recipient address and a payload.

Sending Binary Messages

Binary messages can also be sent with the Wireless Messaging API. The process is very similar to sending TextMessages with the following differences:

  • The TextMessage instance should be replaced by a BinaryMessage instance that uses a MessageConnection.BINARY_MESSAGE
  • The payload for a binary message cannot be a String, but a byte array instead.

For more information, see also Fragmenting a binary message for sending over SMS using Java ME.

See also

180 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.