Wireless Messaging API

WMA

SMS Connection

SMS Client

Send a message by


      String address = "sms://+1234567:5432";
      String msg = "Hello";
      MessageConnection smsconn = 
             (MessageConnection) Connector.open(address);
      TextMessage txtmessage = (TextMessage)smsconn.newMessage(
                MessageConnection.TEXT_MESSAGE);
      txtmessage.setPayloadText(msg);
      smsconn.send(txtmessage)

SMS Server


try {
    String addr =  "sms://:5432";
    MessageConnection conn = (MessageConnection) Connector.open(addr);
    Message msg = null;
    while (someExitCondition) {
        // wait for incoming messages
        msg = conn.receive(); // received a message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            String receivedText = tmsg.getPayloadText();
            // respond with the same text with  Received:
            // inserted in the beginning
            tmsg.setPayloadText("Received: " + receivedText);
            // Note that the recipient address in the message is
            // already correct as we are reusing the same object
            conn.send(tmsg);
        } else {
            // Received message was not a text message, but e.g. binary ...
        }
    }
} catch (Exception e) {
    ...
}

Emulator phone numbers

Complete client




import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;

public class SMSClient extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public SMSClient() {
	display = Display.getDisplay(this);
	displayable = new Form("SMS Client");
    }

    public void startApp() {
	display.setCurrent(displayable);

	try {
	    String address =  "sms://+5550000:5432";
	    String msg = "Hello";
	    MessageConnection smsconn = 
		(MessageConnection) Connector.open(address);
	    TextMessage txtmessage = 
		(TextMessage)smsconn.newMessage(
			       MessageConnection.TEXT_MESSAGE);
	    txtmessage.setPayloadText(msg);
	    smsconn.send(txtmessage);
	} catch (Exception e) {
	    System.out.println(e.toString());
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



Complete server


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;

public class SMSServer extends MIDlet {

    protected Display display;
    protected Displayable displayable;
    protected Form form;

    public SMSServer() {
	display = Display.getDisplay(this);
	displayable = form = new Form("SMS Server");
    }

    public void startApp() {
	display.setCurrent(displayable);

	try {
	    String addr =  "sms://:5432";
	    MessageConnection conn = (MessageConnection) Connector.open(addr);
	    Message msg = null;
	    while (true) {
		// wait for incoming messages
		msg = conn.receive(); // received a message
		if (msg instanceof TextMessage) {
		    TextMessage tmsg = (TextMessage) msg;
		    String receivedText = tmsg.getPayloadText();
		    form.append(new TextField("Message received", receivedText, 
					    10, TextField.ANY));
		} else {
		    // Received message was not a text message, but e.g. binary ...
		}
	    }
	} catch (Exception e) {
	    System.out.println(e.toString());
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



Send and receive

Send and receive client


/**
 * This client differs from SMSClient in that after it sends
 * a message it receives a reply. To do this, it cannot open
 * a client connection, but must open a server connection.
 * Then it sets the client address and sends the message
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;

public class SMSClient2 extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public SMSClient2() {
	display = Display.getDisplay(this);
	displayable = new Form("SMS Client");
    }

    public void startApp() {
	display.setCurrent(displayable);

	try {
	    String myAddress = "sms://:5432";
	    String hisAddress =  "sms://+5550000:5432";
	    String msg = "Hello";
	    MessageConnection smsconn = 
		(MessageConnection) Connector.open(myAddress);
	    TextMessage txtmessage = 
		(TextMessage)smsconn.newMessage(
			       MessageConnection.TEXT_MESSAGE);
	    txtmessage.setPayloadText(msg);
	    txtmessage.setAddress(hisAddress);
	    smsconn.send(txtmessage);

	    // get a reply back. Note, there could be a race
	    // condition here: while this server is waiting for
	    // a reply, someone else could send us a message
	    // which might slip in first... So we should really
	    // check it is from who we expect

	    Message replyMsg = smsconn.receive();
	    if (replyMsg instanceof TextMessage) {
		TextMessage tmsg = (TextMessage) replyMsg;
		String replyText = tmsg.getPayloadText();
		System.out.println("Reply: " + replyText);
	    }
	} catch (Exception e) {
	    System.out.println(e.toString());
	    e.printStackTrace();
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



Receive and send server


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;

public class SMSServer2 extends MIDlet {

    protected Display display;
    protected Displayable displayable;
    protected Form form;

    public SMSServer2() {
	display = Display.getDisplay(this);
	displayable = form = new Form("SMS Server");
    }

    public void startApp() {
	display.setCurrent(displayable);

	try {
	    String addr =  "sms://:5432";
	    MessageConnection conn = (MessageConnection) Connector.open(addr);
	    Message msg = null;
	    while (true) {
		// wait for incoming messages
		msg = conn.receive(); // received a message
		if (msg instanceof TextMessage) {
		    TextMessage tmsg = (TextMessage) msg;
		    String receivedText = tmsg.getPayloadText();
		    form.append(new TextField("Message received", receivedText, 
					    10, TextField.ANY));

		    // now send the message back
		    String replyText = "You sent: " + receivedText;
		    tmsg.setPayloadText(replyText);
		    conn.send(tmsg);
		} else {
		    // Received message was not a text message, but e.g. binary ...
		}
	    }
	} catch (Exception e) {
	    System.out.println(e.toString());
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}




Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Tue Apr 27 12:28:36 EST 2004
Copyright ©Jan Newmarch
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.