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: