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



Ask a Question

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. thank you :)
really helped... :)

View Tutorial          By: Tejas at 2011-03-22 14:17:46

2. I want to download that 5 sms sending program .fro
View Tutorial          By: anwar at 2008-09-08 02:22:06

3. Hi all, i have tried above code, but am getting fo
View Tutorial          By: shalini Iyer at 2008-04-22 01:20:32

4. Your page does not show the text in the proper col
View Tutorial          By: Warron at 2013-04-04 17:50:22

5. Thanks for make me understand about the keyword St
View Tutorial          By: Soumya at 2012-09-18 03:30:01

6. Hi Johnny,

did you check whether th

View Tutorial          By: Frank at 2010-11-13 13:16:57

7. good one
View Tutorial          By: thyagesh at 2011-12-06 05:06:27

8. an excellentttttttt siteee
View Tutorial          By: raza at 2011-04-02 10:39:46

9. Your Crystal report viewer showing all the records
View Tutorial          By: Neel at 2013-06-09 15:02:15

10. good job
by- shiv at 2010-11-26 11:51

View Tutorial          By: shiv kumar bind at 2010-11-25 23:20:28