Comment on Tutorial - How to Send SMS using Java Program (full code sample included) By Emiley J.



Comment Added by : henrynawaung

Comment Added at : 2008-11-14 06:41:13

Comment on Tutorial : How to Send SMS using Java Program (full code sample included) By Emiley J.
Hi All,
I can send sms successfully from Windows XP, by using the java codes.
I found very very simple codes.

Please check this one and inform me if you cannot deploy.

But please be sure all things first. I am trying trying to send in Linux , open SUSE...

1. Please try to send SMS via Hyper Terminal first. Don't try my code before you can send message form hyper terminal first.

(My modem is quite old Wavecom. WMOD2, for that one you can use standard driver, I use the 9600 bps.)

Try to make sure the COM port and the modem setting you chose in HyperTerminal to be matched. Otherwise when you type "AT", it will show funny characters in hyper terminal.

And I am now doing for my customer's SMS authentication program.

import javax.comm.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class SerialWrite implements Runnable, SerialPortEventListener
{
static String output="";
public void run()
{}

static Enumeration portList;
static CommPortIdentifier portId;
static String dest = "+6596123116";
static String messageString = "Hello Testing11";
InputStream inputStream;

static SerialPort serialPort;
static OutputStream outputStream;

public void serialEvent(SerialPortEvent event)
{
switch (event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("Error");
break;
case SerialPortEvent.DATA_AVAILABLE:
{

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
try
{

while ( (line = reader.readLine()) != null)
{

if(line.equalsIgnoreCase("OK") || (line.indexOf("+CMGS") != -1))
{
output=line;

}
Thread.sleep(10);

}
}
catch (Exception e)
{
System.err.println("Error while reading Port " + e);
}
break;

}
} //switch
}

public SerialWrite(SerialPort serial)
{
try
{
inputStream = serial.getInputStream();
try
{
serial.addEventListener(this);
}
catch (TooManyListenersException e)
{
System.out.println("Exception in Adding Listener" + e);
}
serial.notifyOnDataAvailable(true);

}
catch (Exception ex)
{
System.out.println("Exception in getting InputStream" + ex);
}

}

public static void main(String[] args) throws Exception
{
int i=0;
//String line0 = "ATZ\r\n";
String line1 = "AT+CMGF=1\r\n";
//String line2 = "AT+CSCA=\"+6581867612\"\r\n";

//String line3 = "AT+CMGS=""\"" dest + "\""+"\r\n";
//String line3 = "AT+CMGw="+"\""+ args[0] + "\""+"\r\n";
String line3 = "AT+CMGS="+"\""+ args[0] + "\""+"\r\n";
System.out.println("This "+ line3);
//String line4 = messageString;
String line4 = args[1];

portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println("SMS Sending........");
if ( portId.getName().equals("COM1"))
{

System.out.println("SMS Sending....Port Found");
try
{
serialPort = (SerialPort) portId.open("SerialTestApp", 2000);
SerialWrite wr = new SerialWrite(serialPort);
}
catch (PortInUseException e)
{
System.out.println("Port In Use " + e);
}
try
{
outputStream = serialPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("Error writing to output stream " + e);
}
try
{
serialPort.setSerialPortParams(
9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
System.out.println("Error");
}
try
{

//outputStream.write(line0.getBytes());
//Thread.sleep(10);
//while(!output.equalsIgnoreCase("ok"))

//Thread.sleep(10);
//
System.out.println("It seems OK now");

output="";
outputStream.write(line1.getBytes());
while(!output.equalsIgnoreCase("ok"))
Thread.sleep(10);


output="";

//outputStream.write(line2.getBytes());
//while(!output.equalsIgnoreCase("ok"))
//Thread.sleep(10);

output="";

outputStream.write(line3.getBytes());
Thread.sleep(10);
System.out.println("It seems OK now3");
System.out.println("I am here");
System.out.println("This one is output"+output);
for(i=0;i<3;i++)
{

outputStream.write(line4.getBytes());
Thread.sleep(10);
outputStream.write(26);
outputStream.write("\r\n".getBytes());
System.out.println("This one is outputaaaaa "+output);
outputStream.flush();
System.out.println("This one is outputaaaaa "+output);
while(!output.equalsIgnoreCase("ok"))
Thread.sleep(10);
System.out.println("This one is outputdfsdfsdf:"+output);
output="";

//outputStream.write(line5.getBytes());
Thread.sleep(10);
if(output.equalsIgnoreCase("ok"))
System.out.println("Message Sentsfsfasfsafs !!!!!!!!!1");
}

}
catch (IOException e)
{
System.out.println("Error writing message " + e);
}


}
}
}
}

/** show text in the text window
* @param Text text string to show on the display
*/
public static void showText(String Text)
{
System.out.println("TEXT "+Text);
}
}

Try to send my AT commands, line 1, line 2, etc in Hyperterminal first.
Otherwise, you will not see how it works.
That one really works. The original codes show some errors and I change some AT command to send.

To run the code, I think you should have intermediate Java Developing skills,

java SerialWrite "+6596123116" "This one is my message"



I also can send SMS by using minicom from SUSE now.

Please don't ask me silly questions...
If you use the same modem or similar ones, you will definately be able to send SMS by this java codes...

Please try to follow the advice from Sun ..., like that , javax.comm.properties should be already in the jre/lib...etc

Good Luck...!!


View Tutorial



Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Hi,
I am getting this exception..My modem i

View Tutorial          By: puneet at 2010-04-22 10:22:18

2. HI guys i am also studying c++ And now almost mast
View Tutorial          By: Inayat Ullah at 2011-02-08 09:29:44

3. I need a software to send messages to our members.
View Tutorial          By: M V Bhaktha at 2012-05-22 08:19:38

4. How to connect a webpage(which I have created for
View Tutorial          By: sam at 2012-01-24 10:10:52

5. great resource
View Tutorial          By: Anonymous at 2011-04-14 03:14:43

6. i added respective code to the Manifest in every c
View Tutorial          By: niko at 2013-04-19 18:23:41

7. i want full material of java with ds
with

View Tutorial          By: madhulatha at 2009-02-01 16:49:22

8. The code sample in this tutorial is good. It would
View Tutorial          By: Anon at 2009-06-05 03:36:59

9. Very Useful tutorial for beginners.. Thanx
View Tutorial          By: Rohini at 2010-02-23 23:16:16

10. it's cool
View Tutorial          By: RInam Shah at 2011-06-20 02:48:20