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
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
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
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program 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
Archived Comments
1. Its a nice article obviously. Can you put somethin
View Tutorial By: kowser at 2009-10-06 23:44:52
2. very useful information from this page.
View Tutorial By: mahadevi at 2012-04-12 04:38:53
3. nice material
View Tutorial By: ravi at 2008-12-24 00:40:12
4. Hey thanks for your tutorial. But i'm getting an e
View Tutorial By: acc at 2013-07-25 14:30:50
5. Thanks this tutorial helped me :)
View Tutorial By: Anuja at 2012-07-17 06:59:39
6. Unsigned nature of variables
View Tutorial By: Ganesh Chowdhari at 2008-08-12 06:33:12
7. nice tutorial
View Tutorial By: Amit at 2010-04-16 04:48:52
8. Can you explain when the user input through the ab
View Tutorial By: mujmil at 2008-02-11 18:46:00
9. good......
View Tutorial By: Sudip Khatiwara at 2010-04-05 22:31:06
10. Thank you very much for such a nice topic
View Tutorial By: CHANDAN at 2012-11-01 04:40:02