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 new java, and i want to become gud develo
View Tutorial          By: amit at 2012-02-16 12:21:25

2. it's very good example to understands overloading
View Tutorial          By: Rakesh Sharma at 2011-11-18 06:37:49

3. sir , i am new in j2me and i want to learn more s
View Tutorial          By: vipin Luhach at 2012-01-19 06:43:27

4. Real Life example of using Volatile
*******

View Tutorial          By: Mayank at 2011-01-25 15:44:21

5. thnx
View Tutorial          By: Prakash at 2011-07-27 03:05:17

6. it very usefull to introduse the j2me to me

View Tutorial          By: Sudhir at 2011-08-31 13:05:40

7. Simple Codes please... I just wonder why does my p
View Tutorial          By: Joseph at 2009-11-12 05:37:50

8. good tutorial
View Tutorial          By: mohang at 2011-12-24 05:24:56

9. Support for serial ports is platform dependent and
View Tutorial          By: Sal at 2009-09-15 15:51:55

10. The code gives the following exception :-
<

View Tutorial          By: Lourdu Felix at 2009-01-26 03:37:53