Jimmy’s Blog – Send & Receiving SMS on specific Port with J2ME Application
by jimmy on Feb.10, 2011, under English, Java, Programming
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 Reveiver in Simulator
Related posts:
- Java ME (J2ME) JSON Implementation Tutorial/Sample
- Java Application – Make sure only single/one instance running – with File Lock and ShutdownHook
- Basic LWUIT tutorial/sample with Eclipse Pulsar
- Jimmy’s Blog – Slide Number Puzzle Game with Echo2 Framework
- Java ME (J2ME) JSON Implementation For Array Object
February 10th, 2011 on 7:40 pm
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.
February 11th, 2011 on 7:32 pm
February 24th, 2011 on 5:31 pm
what is package poc.jwork.j2me.sendrecvsms; ?
where can get it ?
February 28th, 2011 on 3:33 pm
@yook : that is the package of the current Class that I created. You can set it to any package you wanted, just match it with your file location
March 25th, 2011 on 3:15 pm
Thanks for ur blog, but could you tell me that how to send the sms to java os mobile on a specific port.
March 25th, 2011 on 3:48 pm
@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)
March 25th, 2011 on 5:21 pm
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.
March 25th, 2011 on 5:42 pm
@NPRS:
Yes, kannel is for Linux platform.
I don’t experience in AT Commands so I don’t think I can help you.
I was stumble to this blog : http://adywicaksono.wordpress.com/2008/02/10/sending-sms-to-j2me-application/
There’s PDU & AT Commands sample.
Or you can try NowSMS trial just for prove of concept, it also have HTTP call to trigger binary sms.
March 25th, 2011 on 5:50 pm
Thanks for ur comments!!!. I seen that site but it had not given the AT COMMANDS with port address to send the sms.
April 21st, 2011 on 9:22 pm
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..
May 3rd, 2011 on 1:23 am
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
June 4th, 2011 on 6:01 pm
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.
June 8th, 2011 on 7:02 pm
@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.
June 21st, 2011 on 8:08 pm
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
June 22nd, 2011 on 5:04 pm
@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.
June 29th, 2011 on 12:22 am
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
July 2nd, 2011 on 12:03 am
@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.
July 17th, 2011 on 12:09 am
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!
July 20th, 2011 on 6:56 pm
@chocolate:
To bad current MIDP 2.x is not supporting background process.
Another way to implement this idea is by using MIDP push registry (http://developers.sun.com/mobility/midp/articles/pushreg/). The concept is by registering the J2ME application to the OS/platform so whenever there’s an incoming SMS to specific port the application is called.
This give me idea to create tutorial about this
July 24th, 2011 on 12:38 am
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
July 24th, 2011 on 7:32 am
@Neeraj: What phone are you using?
Have you try other port?
August 1st, 2011 on 1:03 am
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
August 1st, 2011 on 6:47 pm
@Neeraj:
I’m not sure how to solve this issue since I don’t have Nokia 5610.
Btw is it working on emulator?
August 30th, 2011 on 2:53 am
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.
September 2nd, 2011 on 5:15 pm
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
September 7th, 2011 on 2:53 pm
@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.
September 10th, 2011 on 3:30 pm
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.
September 10th, 2011 on 6:15 pm
Truly enlightening appreciate it, I do think your trusty audience would certainly want even more stories such as this carry on the great content.
September 11th, 2011 on 2:14 am
Extraordinarily educative thanks, It is my opinion your audience would probably want a great deal more posts similar to this continue the excellent effort.
September 11th, 2011 on 5:44 pm
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.
September 12th, 2011 on 9:47 pm
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
September 13th, 2011 on 10:31 pm
@jojie:
Can you explain more detail about the problem?
Is it the IDE (Eclipse/Netbeans/other)? the JDK installation? or the emulator?
September 21st, 2011 on 11:45 am
hi,
no action is performed on listen command, will you suggest me what to do. I am running an application on nokia 5230.
September 26th, 2011 on 5:42 pm
hi can you email me?
reyes.josea@yahoo.com
i have hard time understanding this
September 26th, 2011 on 6:34 pm
@amit: do you mean the commandAction is not called when the command pressed ?
September 27th, 2011 on 6:44 am
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!
September 29th, 2011 on 5:09 pm
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
September 29th, 2011 on 5:09 pm
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
October 15th, 2011 on 12:13 am
@Vani: I’m not sure. Can you trace on what method causing the IOException?
Is it when receiving SMS or sending? or ?
October 19th, 2011 on 12:45 pm
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 ???
October 28th, 2011 on 1:58 pm
hey this example is working perfectly
i used it in my project, i achieved what i wanted
thank you jimmy
November 21st, 2011 on 12:10 am
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.
November 24th, 2011 on 6:01 am
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
December 18th, 2011 on 9:51 am
Thank for your work Jimmy
December 23rd, 2011 on 11:56 pm
@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)
February 21st, 2012 on 10:45 pm
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
May 7th, 2012 on 12:09 am
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
May 7th, 2012 on 6:32 am
@Ilic:
It mean the variable listener is not exist on that class.
Are you sure you copy the whole ListenSMS class correctly?
May 9th, 2012 on 2:45 am
Thanks,
May I ask some question about how I know which port is working for Nokia c1-01?
May 9th, 2012 on 9:40 am
@Ilic : I think most random number should be working, you just need to test it.