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 Reveiver in Simulator
Related posts:
- Java ME (J2ME) JSON Implementation Tutorial/Sample
- Basic LWUIT tutorial/sample with Eclipse Pulsar
- Jimmy’s Blog – Slide Number Puzzle Game with Echo2 Framework
- Java Application – Make sure only single/one instance running – with File Lock and ShutdownHook
- Jimmy’s Blog – ISO 8583 Tutorial – Build and Parse ISO Message using JPOS library
May 9th, 2012 on 2:55 pm
Send and receive it should be same port?
May 20th, 2012 on 1:01 pm
@Ilic: yes, if you send to port 5000 the listener need listen to port 5000 to be able to catch the message.
May 24th, 2012 on 10:38 pm
thanks, that was useful for me
May 25th, 2012 on 1:56 pm
Thanks,
short,simple and straight forward
May 26th, 2012 on 11:14 pm
@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.
June 1st, 2012 on 8:44 am
@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)
June 24th, 2012 on 5:22 pm
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
June 25th, 2012 on 3:22 pm
@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
September 19th, 2012 on 5:09 pm
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
September 25th, 2012 on 3:53 pm
@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
September 26th, 2012 on 12:32 am
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
October 16th, 2012 on 1:32 pm
@Aqeel:
It’s device specific so it can be different from one device to another
November 23rd, 2012 on 7:01 pm
Awesome Jimmy! How would one receive multiple concatenated SMS messages, treating them all part of the same message? Many thanks
January 24th, 2013 on 11:34 am
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
May 15th, 2013 on 9:22 am
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.