Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

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

From Wiki
Jump to: navigation, search
Featured Article
10 Nov

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.



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, a field for typing the port number (optional), 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. The port number field can only receive up to 4 digits.

        mainForm = new Form("SMS Example");
smsNumber = new TextField("Phone number", null, 20,
smsText = new TextField("Text", null, 160, TextField.PLAIN);
port = new TextField("Port", null, 4, TextField.NUMERIC);
sendCommand = new Command("Send", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 0);

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 only appended to the address if the user has provided an non empty numeric entry. A connection is opened to the newly created address and a message of type TEXT_MESSAGE is associated with the address. A payload is added to the message by calling the setPayloadText method and passing as argument the content of the second TextField. Finally the method sent() is called, which receives as argument the TextMessage instance. Appropriate handling of Exceptions follows, along with a print screen message, confirming the completion of the operation. The sending operation takes place inside a thread, because the call is protected and Nokia Asha software platform, does not allow protected calls directly initiated from a CommandAction, without being wrapped in a Thread.

private void handleSendCommand() {
// Send the message on its own thread of execution
Thread smsThread = new Thread() {
public void run() {
try {
String number;
//Obtain the destination address
if(port.getString().equals("")) {
number = "sms://" + smsNumber.getString();
else {
number = "sms://" + smsNumber.getString() + ":" + port.getString();
// Obtain the specified text
String text = smsText.getString();
connection = (MessageConnection);
message = (TextMessage)connection.newMessage(MessageConnection.TEXT_MESSAGE);
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) {
mainForm.append("Security Exception!\n");

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

This page was last modified on 10 November 2013, at 19:25.
117 page views in the last 30 days.