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

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 receivedn");
			}
		} 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

68 comments

  1. I would like to thank you such a lot of for your job you have made in writing this posting. I am hoping the same most reliable work by you in the future as well.

  2. @NPRS:
    Depends on from which platform you want to send the sms.
    My example above is using J2ME to send SMS on specific port.
    If you plan to use GSM modem / connected directly to telco system, popular SMS Gateway like Kannel support Binary SMS. If I’m not mistaken you can setup the SMS port destination in the SMS’ UDH (user data header)

  3. Thanks for Reply!!!
    I am working on j2me application that deals with incoming sms on mobile phone, but as we java mobile os doesn’t allow to use the msg in any application. So what i m dong is that i’m listening on a port(e.g. 50000) to get the sms for my application. But the condition arises that how my GSM modem could send sms to that port address of mobile (as accept mobile number and message to send). U talked abt kannel but as per my knowledge its for LINUX n don’t want to go for linux. I’m working still to modify UDH of message format to send the sms by attaching “destination mobile port” with it. If u know abt using UDH or PDU format pls tell me, using AT COMMANDS.

  4. Thanks for ur comments!!!. I seen that site but it had not given the AT COMMANDS with port address to send the sms.

  5. Salam!!

    I am beginner of j2me development..
    and my question is quote of silly nature..

    but this code is going to be pasted on two different midlets???
    Kindly guide..

  6. i’m working with net beans and some errors appear in the class “SMSReceiver” with these for example
    “formReceiver.setCommandListener(this);

    display = Display.getDisplay(this);” with “this” advising to convert it to CommadListener

    any solution?
    thanks

  7. Thank you very much,

    I used this code directly in my netbens. i compile the package of this classes it compiled successfully.than i tried it in my mobile after installation i am able to send the message.so

    I conclude this code is working fine in mobile you can send message thanks again.

  8. @Maheen:
    This is just poc (prove of concept) how you send a SMS from MIDlet that will be received by other MIDlet.
    You can see this as 2 different application.

    @Ismael:
    this is pointing to the current MIDlet. It needed to get active Display object, so we can set which Form need to be showed to user.

  9. Hi, thanx for such a nice post. tere is not even a single error in your code i am able to send sms but i am still unable to receive sms :( plz help me out

  10. @Shan:
    Just to make sure you don’t miss understand:
    The receiving SMS app is only capture the SMS send by the sending SMS app.
    So it cannot capture normal SMS.

    So you need 2 phone to test it.
    1st phone installed the send-sms.
    2nd phone installed the receive-sms.

    Run the receive-sms app first and start listening, then run the send-sms app and send sms to same port set on receive-sms.

  11. This exampleis clear to me but when i am trying to execute it, i have not been able to do it successfully. I mean to say that when we send the message through SMSSender how teh receiver is going to receive it, whether it will be done in the same window or i have to open a new window. how to implement it successfully, Please let me know

  12. @Mukul Chandha:
    If you’re talking about emulator, you need to run 2 emulator. 1 running the SMSSender MIDlet, another one running the SMSReceiver MIDlet.
    So yes, you will have 2 window emulator.

    Our you can install it on 2 phones.

  13. I downloaded your JAR file and install on my phone (6120c – Symbian S60 3rd). I run 2 app in same time and try to send SMS to this phone. It’s work, but I have one question here:
    On regular phone (S40 phone, no symbian OS, only support J2me), SMSReceiver cannot run any time (background process) to catch SMS incoming that sent by SMSSender. So, how to develop a j2me app to Send & Receive SMS that can read SMS in “Inbox”. Any way to implement my idea? Thank in advance!

  14. Hello Jimmy

    Thank you for the code. I tried running it on my phone and got this error

    Security java/lang/secutiryexception not allowed to open the connection. Could you please help

    Thank you
    Neeraj

  15. Hello Jimmy,

    Thank you very much for responding

    I am using Nokia 5610. I tried with other ports like 5001, 6000 but it gives the same message. I also got my phone unlocked today and tried the application again. I am getting the same message

    Regards
    Neeraj

  16. Hi Jimmy,
    You said : The receiving SMS app is only capture the SMS send by the sending SMS app.
    So it cannot capture normal SMS.

    So, is there any way to capture normal SMS?
    Sender doesnt have SMS App.

    Thanks in advance.

  17. what is the port number used to send messages..my j2me pgm works it has no error..but am confused with the port number..do give reply..what is the port number..i used 2923,5511.5000 but nothing works..do reply

  18. @Ramazan:
    Unfortunately with current MIDP 2 features, there’s no way to capture normal SMS. Or read the SMS inbox. cmiiw, there’s also no jsr yet for this feature.
    You will need to develop application that have more access to the OS, like symbian programming for symbian phones.

    @Ragesh:
    You test use any port. Try to send it to the phone that doesn’t have receiver sms application, it will received like normal SMS.

  19. Fantastically entertaining bless you, There’s no doubt that your current visitors will probably want a good deal more well written articles similar to this continue the great content.

  20. Truly enlightening appreciate it, I do think your trusty audience would certainly want even more stories such as this carry on the great content.

  21. Extraordinarily educative thanks, It is my opinion your audience would probably want a great deal more posts similar to this continue the excellent effort.

  22. Genuinely educative bless you, It is my opinion your trusty audience would probably want a whole lot more stories like this carry on the great content.

  23. Hi jimmy. I’m new in j2me, but I have something problem for the requirements that I will use to run the program. thus anything that I need to do? Thank. Please send to my email. Thank you so much

  24. uhm what ide will i use on this? eclipse or netbeans? i prefer netbeans, do you have any tutorials?? or add some print screens please, my prof want me to learn java in 5 days so i want to learn :)pls add some step by step images on how to create this, whether in netbeans or eclipse pls include also the version of the IDE thanks my friend!

  25. Thanks for the nice example. I tried to run the jar on a nokia emulator(series 4o, 5th edition), but I get an IOException WDP Error. When I run the jar on sun emulator, it works well. Can you please tell suggest something?
    Thanks,
    Vani

  26. Thanks for the nice example. I tried to run the jar on a nokia emulator(series 40, 5th edition), but I get an IOException WDP Error. When I run the jar on sun emulator, it works well. Can you please tell suggest something?
    Thanks,
    Vani

  27. Hi jimmy,
    Thanx.. I was able to run your application without any errors. I just have a confusion- does this message make use of Pushregistry to send sms. Can we say that your code is based on pushregistry ???

  28. I’m a bit busy these days.. might not respond to you guys fast.

    @Priyanka:
    This sample is not push-registry, although you can combined it.
    Push-registry will allow the MIDlet register itself to the phone OS so if there incoming SMS to specific port, the MIDlet will be called.

  29. hi Jimmy,
    i need some help in resolving port address problems.i want to send an sms from my midlet to a gsm modem via a specific port.the modem is in com15 and the modem number let’s say is 123456789 and i try the following:
    msg.setAddress(“sms://+123456789:COM15″);
    my question is how do i set the com15(or any other com for that matter) to a more appropriate format e.g 1234 or 5000 so as to send the sms o the gsm modem.

    and also the gsm modem is connected to an sms gateway server that processes the incoming messages and returns a reply(sms) to the midlet that sent the sms..so i was wondering if i have to use push registry for the midlet to receive the sms .thanks

  30. @chavo:
    I don’t experience in programming for GSM modem but I think it’s doesn’t work like J2ME.
    If you already connect to the GSM modem, depends on library/programming language you used, you should able to get sms receive event on any port. So you don’t need to specify which port.

    I remember using NowSMS/Kannel/forgot the name, when sms received it trigger HTTP call.

    For sending from GSM modem to J2ME also depends on how you connect to GSM modem, worst case you need to build binary SMS, create the UDH(header) and specify the port you want to use (NowSMS have this feature, other should have too I think)

  31. thank for the script,

    sms sent ok but at receiving mobile it says that “Message can not be Displayed” why ? please advise

    thanks a lot

  32. Hi, I’m beginer of j2me, i’m try this code but found this “error: cannot find symbol this.listener = listener;”. how to fix? thanks a lot

  33. @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.

  34. @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)

  35. 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

  36. @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

  37. 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

  38. 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

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

  40. 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

  41. 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.

  42. 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

  43. Cane you and anyone else give the code of sms send mobile app in j2me in which not used port need phone app code that send sms……

  44. hi jimmy my application smssender not show any thing on emulator screen and mobile is not installing it please help me thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP-SpamFree by Pole Position Marketing