Now lets discuss on this topic::
That is how do i send sms through a java program...
For this u need the following::
1.)Connect ur phone to ur computer using bluetooth and check out the port number to which it is connected through the phone and modem options in the control panel.
2.) Download comm.jar. It comes with the javaComm api , The official API for serial communication in Java.
Several things to do before running the code is::
1.) include the comm.jar file in your library.
2.) copy the win32com.dll file from the javacomm folder that u will download and paste it into the jre/bin folder of jdk.
3.) Copy the javax.comm.properties file and paste it into the jdk/lib folder.
Now in the following code the AT commands are used to send the commands to the mobile phone through the port number mentioned in the code, through which the mobile is connected to the computer..
the AT commands make the phone send an sms to the mobile number mentioned in the code..
The code is as follows::
import javax.comm.*;
import java.util.*;
import java.io.*;
public class Main {
public Main() {
}
public static void main(String[] args) {
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null; // will be set if port found
String wantedPortName = "COM5";
while (portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
break;
}
}
if(portId == null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
SerialPort port = null;
try {
port = (SerialPort) portId.open(
"name", // Name of the application asking for the port
10000 // Wait max. 10 sec. to acquire port
);
} catch(PortInUseException e) {
System.err.println("Port already in use: " + e);
System.exit(1);
}
try
{
port.setSerialPortParams(
115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch(Exception e)
{
System.out.println(e.toString());
}
BufferedReader is = null;
PrintStream os = null;
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
}
catch (IOException e) {
System.err.println("Can't open input stream");
is = null;
}
try{
os = new PrintStream(port.getOutputStream(), true);
}
catch (IOException e) {
System.err.println("Can't open output stream");
is = null;
}
os.print("AT");
os.print("\r\n");
try
{
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
}
catch (IOException e) {
System.err.println("Can't recieve input signals");
}
os.print("AT+CMGF=1");
os.print("\r\n");
try
{
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
}
catch (IOException e) {
System.err.println("Can't recieve input signals");
}
char qu=34;
char cz=26;
os.print("AT+CMGS="+qu+"+919894198941"+qu+"\r\n");
os.print("hello india"+cz+"\r");
try
{
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
}
catch (IOException e) {
System.err.println("Can't recieve input signals");
}
port.close();
}
} //End Of class......
Thats it..........
1 comment:
Thank you very much.......
Post a Comment