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. Hi all, i have tried above code, but am getting fo
View Tutorial          By: swathi at 2010-06-24 05:18:28

2. This code is only for send sms? can it also alert
View Tutorial          By: abdul kadir at 2009-10-23 05:37:46

3. Good explanation about Iterator. But Object declar
View Tutorial          By: Prasad at 2008-12-22 23:20:59

4. Hi..thx for the hadoop info..i m learning hadoop..
View Tutorial          By: Kirti at 2015-10-26 11:42:02

5. HOW DO I FIND MY COMM PORT NAME SO THAT I CAN SPEC
View Tutorial          By: RUTUJA at 2013-10-01 18:27:59

6. really very excellent example....ThanQ...:)
View Tutorial          By: Tulasi at 2010-08-17 00:41:21

7. sample program in packages and interface combined
View Tutorial          By: priya at 2009-09-10 05:42:31

8. This program little hard to understand ..please gi
View Tutorial          By: sarav at 2011-02-17 05:02:46

9. Many thanks for this tutorial - it has saved me so
View Tutorial          By: Ehsan at 2011-11-20 23:12:21

10. Hi professional,
I tried the above code but

View Tutorial          By: Srinivas at 2008-05-05 23:32:13