JAVA Program For Processing SMS Messages Krishna Akkulu The paper describes the Java program implemented for the MultiModem GPRS wireless modem. The MultiModem offers standards-based quad-band GSM/GPRS Class 10 performance. Also the GPRS wireless modem with an Ethernet interface provides shared Internet access with one IP address. The advantage of Ethernet interface is any machine which is on network can communicate to with the modem. The picture shows MultiModem GPRS wireless modem. Generally programmers use AT commands through Telnet to send and receive the messages from the Modem. But most of the software applications prefer to send or receive the commands through programs depends on certain events occur in an application. Example let us say a software application determines the room temperature increased or earth quake or fire alert occurred, then the program has to notify, the operator by sending SMS message etc. In such situations telnet program may not be good a solution. So, in this paper the telnet application has been simulated with Java Socket program. The present Java program demonstrates sending and receiving SMS messages to/from cell phones.
The AT commands used for receiving SMS messages are: 1) at+cmgf=1 (The command sets to text mode) 2) at+cnmi=2,2,0,0,0 (selects the procedure for message reception from the network) Now the Modem is ready to receive the SMS messages. The AT commands used for Sending SMS messages are: 1) at+cmgf=1 (if already set Text mode then not required.) 2) at+cmgs="1234567890" (type phone number and then press Enter) (now > symble will be displayed. After the symbol type message) 3) > This is test message (after the message hit <CTRL+Z> to send the message) Then the following message will be received. That means Modem indicating the SMS message has been sent successfully. +CMGS:16 OK The Java Class name is GPRSMultiModem. The class implements Runnable interface to achieve multithreading feature. The Main APIs in the program are. 1) Connect(InetAddress ModemAddress, int Port) With given IPAddress and Port number connects to the Modem. 2) ReceiveMsg() This method uses DataInputStream s readline() to read the messages from Modem. Also to the messages continuously ReceiveMsg() methods implemented as Thread function. 3) SendMsg(String strmsg, boolean bvalue) This is a general API used to send all messages to Modem. Except for SMS messages for all other messages bvalue is false. SMS Messages needs <CTRL+Z> to be hit at the end of messages. So this will be achieved with DataOutputStream s writebyte(0x1a) API. 0x1A represents <CTRL+Z>. writebyte(0x1a) will be invoked only when bvalue is true. 4) PrepareSMSMsg (String strphone, String strmsg) The method First send "at+cmgs=\""+strphone+"\"" (at+cmgs = "1234567890") Then Modem send response > Now call SendMsg(strMsg, true); 5) CloseSocket() Close Socket Connection.
*************** * Java program listing is given below * **************** import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.net.inetaddress; import java.net.socket; import java.net.unknownhostexception; public class GPRSMultiModem implements Runnable String m_modemaddress ="172.16.1.242"; int m_port =5000; DataInputStream is = null; Socket MultiModemSocket = null; DataOutputStream os = null; boolean bconnected =false; boolean bwaittorecv =true; Thread threadobj= new Thread(this,""); *main method *@param args public static void main(string[] args) GPRSMultiModem objgprsmultimodem = new GPRSMultiModem(); InetAddress m_modemaddress = InetAddress.getByName( objgprsmultimodem.m_modemaddress); objgprsmultimodem.connect(m_modemaddress, objgprsmultimodem.m_port); Thread.sleep(1000); objgprsmultimodem.threadobj.start(); objgprsmultimodem.sendmsg("at+cmgf=1\r\n",false); Thread.sleep(2000); objgprsmultimodem.sendmsg( "at+cnmi=2,2,0,0,0\r\n",false); Thread.sleep(1000); objgprsmultimodem.preparesmsmsg("4084449451", "Hello Krishna");
catch(exception e) System.out.println("Modem Error "); *run method of Runnable interface *@param args public void run() ReceiveMsg(); *Connect method of Runnable interface *@param InetAddress, Port public void Connect(InetAddress ModemAddress, int Port) MultiModemSocket = new Socket( ModemAddress.getHostName(), Port); os = new DataOutputStream( MultiModemSocket.getOutputStream()); is = new DataInputStream( MultiModemSocket.getInputStream()); bconnected=true; catch (UnknownHostException e) bconnected=false; System.out.println("Don't know about host - "); bconnected=false; System.out.println("Couldn't get I/O for the connection to: hostname - "); ** * Close the Socket connection * public void CloseSocket()
os.close(); is.close(); MultiModemSocket.close(); bconnected=false; System.out.println("Close Sockets"); System.out.println(e.getLocalizedMessage()); ** * Receive Messages from MultiModem. ** public void ReceiveMsg() while(bwaittorecv) String responseline=""; SendMsg("\r",false); int iter =0; if (is!= null) while (iter<20) responseline = is.readline(); Thread.sleep(1000); if(!responseline.isempty()) break; iter++; catch (UnknownHostException e) System.out.println("Trying to connect to unknown host: " + e.getlocalizedmessage());
System.out.println(e.getLocalizedMessage()); catch (Exception e) System.out.println(e.getLocalizedMessage()); if(!responseline.isempty()) System.out.println("responseLine = "+responseline); *Connect method of Runnable interface *@param String, boolean public void SendMsg(String strmsg, boolean bvalue) if (MultiModemSocket!= null && os!= null && is!= null) if(bvalue) os.writebytes(strmsg); os.writebyte(0x1a); else os.writebytes(strmsg); catch (UnknownHostException e) System.out.println("Trying to connect to unknown host: " + e.getlocalizedmessage()); System.out.println("IOException: " + e.getlocalizedmessage()); ******** * Prepare SMS Msg Command *@param String, String ******** public void PrepareSMSMsg(String strphone, String strmsg)
String Str ="at+cmgs=\""+strphone+"\""; SendMsg(Str, false); Thread.sleep(5000); SendMsg(strMsg, true); catch (Exception e) System.out.println("IOException: " + e.getlocalizedmessage());