Sie sind auf Seite 1von 7

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)
{
try
{
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)
{
try
{
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 - ");
}
catch (IOException e)
{
bconnected=false;
System.out.println("Couldn't get I/O for the connection to:
hostname - ");
}
}
/******************************************
* Close the Socket connection
*****************************************/
public void CloseSocket()
{
try
{
os.close();
is.close();
MultiModemSocket.close();
bconnected=false;
System.out.println("Close Sockets");
}
catch (IOException e)
{
System.out.println(e.getLocalizedMessage());
}
}
/******************************************
* Receive Messages from MultiModem.
******************************************/
public void ReceiveMsg()
{
while(bWaitToRecv)
{
String responseLine="";
SendMsg("\r",false);
int iter =0;
try
{
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());
}
catch (IOException e)
{
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)
{
try
{

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());
}
catch (IOException e)
{
System.out.println("IOException: " +
e.getLocalizedMessage());
}
}
}
/************************************************
* Prepare SMS Msg Command
*@param String, String
************************************************/
public void PrepareSMSMsg(String strPhone, String strMsg)
{
try
{
String Str ="at+cmgs=\""+strPhone+"\"";
SendMsg(Str, false);
Thread.sleep(5000);
SendMsg(strMsg, true);
}
catch (Exception e)
{
System.out.println("IOException: " +
e.getLocalizedMessage());
}
}
}

Das könnte Ihnen auch gefallen