Jimmy's Blog

Jimmy’s Blog – Send & Receiving SMS on specific Port with J2ME Application

by jimmy on Feb.10, 2011, under English, Java, Programming

[Switch to Mobile Edition]

It’s been a while since I write an article, personal and work have been taken my time much lately.

What I will write is not a new things, maybe a lot of article already write this kind of feature. But yesterday I see this kind of project request in freelancer.com so I decide to create a simple sample and write an article about it.

Java Mobile (J2ME) application have a capability to send or receive SMS, but the phone need to support JSR-120 (Wireless Messaging API – WMA 1.0) or JSR-205 (WMA 2.0)
How to check if the phone is supporting this? Easy way if the phone’s brand website. For example for Nokia E72 we can see the technical specifications at http://developer.nokia.com/Devices/Device_specifications/E72/
If your phone doesn’t have WMA library the application will prompt error (the error can be different from one phone to another)

Sending SMS with J2ME is simple, one thing that programmer can forget is about putting it on another thread. To prevent a dead lock on sending, we need to create a thread to execute the sending.
Another thing is we can specifically send the SMS to specific port, which is important in this J2ME case. Because in J2ME we can only listen SMS on specific port (cannot on default port).

The sample is simple, with basic J2ME programming skill you should able to understand it. If you don’t just drop me a comment :)

Source code:

package poc.jwork.j2me.sendrecvsms;
 
import java.io.IOException;
 
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;
 
public class SMSSender extends MIDlet implements CommandListener {
 
	private Form formSender = new Form("SMS Sender");
	private TextField tfDestination = new TextField("Destination", "", 20, TextField.PHONENUMBER);
	private TextField tfPort = new TextField("Port", "5000", 6, TextField.NUMERIC);
	private TextField tfMessage = new TextField("Message", "message", 150, TextField.ANY);
	private Command cmdSend = new Command("Send", Command.OK, 1);
	private Command cmdExit = new Command("Exit", Command.EXIT, 1);
	private Display display;
 
	public SMSSender() {
		formSender.append(tfDestination);
		formSender.append(tfPort);
		formSender.append(tfMessage);
		formSender.addCommand(cmdSend);
		formSender.addCommand(cmdExit);
		formSender.setCommandListener(this);
 
		display = Display.getDisplay(this);
	}
 
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	}
 
	protected void pauseApp() {
 
	}
 
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(formSender);
	}
 
	public void commandAction(Command c, Displayable d) {
		if (c==cmdSend) {
			SendMessage.execute(tfDestination.getString(), tfPort.getString(), tfMessage.getString());
		} else if (c==cmdExit) {
			notifyDestroyed();
		}
	}
 
}
 
class SendMessage {
 
	public static void execute(final String destination, final String port, final String message) {
 
		Thread thread = new Thread(new Runnable() {
 
			public void run() {
				MessageConnection msgConnection;
				try {
					msgConnection = (MessageConnection)Connector.open("sms://"+destination+":" + port);
					TextMessage textMessage = (TextMessage)msgConnection.newMessage(
							MessageConnection.TEXT_MESSAGE);
					textMessage.setPayloadText(message);
					msgConnection.send(textMessage);
					msgConnection.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
 
		thread.start();
	}
 
}
package poc.jwork.j2me.sendrecvsms;
 
import java.io.IOException;
import java.io.InterruptedIOException;
 
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;
 
public class SMSReceiver extends MIDlet implements CommandListener, MessageListener {
 
	private Form formReceiver = new Form("SMS Receiver");
	private TextField tfPort = new TextField("Port", "5000", 6, TextField.NUMERIC);
	private Command cmdListen = new Command("Listen", Command.OK, 1);
	private Command cmdExit = new Command("Exit", Command.EXIT, 1);
	private Display display;
 
	public SMSReceiver() {
		formReceiver.append(tfPort);
		formReceiver.addCommand(cmdListen);
		formReceiver.addCommand(cmdExit);
		formReceiver.setCommandListener(this);
 
		display = Display.getDisplay(this);
	}
 
	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
 
	}
 
	protected void pauseApp() {
 
	}
 
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(formReceiver);
	}
 
	public void commandAction(Command c, Displayable d) {
		if (c==cmdListen) {
			ListenSMS sms = new ListenSMS(tfPort.getString(), this);
			sms.start();
			formReceiver.removeCommand(cmdListen);
		} else if (c==cmdExit) {
			notifyDestroyed();
		}
	}
 
	public void notifyIncomingMessage(MessageConnection conn) {
		Message message;
		try {
			message = conn.receive();
			if (message instanceof TextMessage) {
				TextMessage tMessage = (TextMessage)message;
				formReceiver.append("Message received : "+tMessage.getPayloadText()+"\n");
			} else {
				formReceiver.append("Unknown Message received\n");
			}
		} catch (InterruptedIOException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
}
 
class ListenSMS extends Thread {
	private MessageConnection msgConnection;
	private MessageListener listener;
	private String port;
 
	public ListenSMS(String port, MessageListener listener) {
		this.port = port;
		this.listener = listener;
	}
 
	public void run() {
		try {
			msgConnection = (MessageConnection)Connector.open("sms://:" + port);
			msgConnection.setMessageListener(listener);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
}

Screenshot:

SMS Sender in simulator

SMS Sender in simulator


SMS Reveiver in Simulator

SMS Reveiver in Simulator

Build:
JAD FILE
JAR FILE

Related posts:

  1. Java ME (J2ME) JSON Implementation Tutorial/Sample
  2. Basic LWUIT tutorial/sample with Eclipse Pulsar
  3. Jimmy’s Blog – Slide Number Puzzle Game with Echo2 Framework
  4. Java Application – Make sure only single/one instance running – with File Lock and ShutdownHook
  5. Jimmy’s Blog – ISO 8583 Tutorial – Build and Parse ISO Message using JPOS library

:, , , , , , , ,

66 Comments for this entry

  • Ilic

    Send and receive it should be same port?

  • jimmy

    @Ilic: yes, if you send to port 5000 the listener need listen to port 5000 to be able to catch the message.

  • boutfounast

    thanks, that was useful for me

  • Moein

    Thanks,
    short,simple and straight forward

  • ilic

    @jimmy: Thank a lot. I need some help about, how can i receive SMS only my sending port? (other port exp: SMS from normal send/receive …). How can I block SMS other port during run my application? thanks.

  • jimmy

    @ilic:
    I don’t think it possible with J2ME, that’s why the SMS sender need to use application that define the SMS port. So your application cannot receive all normal SMS (if this is what you plan to do)

  • Farz

    hi
    Thanks for your sample, my question is the way to have both send and receive in one application! I had an example with them in a same , but on my phone it didn’t work. I need to have both in one class which has two threads

  • jimmy

    @Farz:
    Just combine the 2 class above.
    I don’t really have time to test it now. So try above code first and let us know if there’s error/issue

  • Aqeel

    Dear i am using is with my Nokia 2330c Nokia with OS Series 40, i can send sms using port 5000, but cannot receive inbox messages on neither port 5000 nor 0, please help me its very urgent and very important.

    Regards

  • jimmy

    @Aqeel: I don’t have 2330c, but have you tried other random number? Don’t use 0 it will always failed.
    Try port number like 1234, 3333, etc

  • Aqeel

    thanks jimmy for your kind reply,,,,, i tried your 1234, 3333, 5001, 1111 etc but still its not working can you give me a detail port from-to should i try to make it working, thanks any more help you have plz do it with me thanks again

  • jimmy

    @Aqeel:
    It’s device specific so it can be different from one device to another

  • Ted

    Awesome Jimmy! How would one receive multiple concatenated SMS messages, treating them all part of the same message? Many thanks

  • Manikandan

    When I’m receiving the Textmessage with hexadecimal values, it displays likes small squares… I not understood that what was happened… Kindly help in this issue

  • winnie

    Hi, I am newbie of J2ME. I am not sure I be able to send SMS but I cannot receive the SMS that I sent. When I at SMSReceiver, I click listen with same port with SMSSender, it does not happen anything.

  • b

    hello
    please help me!please help me!please help me!
    1-why program is in 2 package?how make it in 1 package?
    2-what maeans “class ListenSMS extends Thread”?in code
    3-what means throws MIDletStateChangeException?
    4-please give me information about:TextMessage textMessage = (TextMessage)msgConnection.newMessage(
    MessageConnection.TEXT_MESSAGE);
    5-what is :Thread?
    thanks

Leave a Reply

Spam Protection by WP-SpamFree

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...