pls send me the code to send sms from pc to mobile via GSM modem programmed in JAVA
<p>here is the answer.....
can u pls help me to retrieve message from mobile to java program...
import java.io.*;
import java.util.*;
import javax.comm.*;</p>
<p>public class GSMConnect implements SerialPortEventListener,
CommPortOwnershipListener {</p>
<p> private String comPort = "COM3"; // This COM Port must be connect with GSM Modem or your mobile phone
private String messageString = "";
private CommPortIdentifier portId = null;
private Enumeration portList;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private SerialPort serialPort;</p>
<p> /** Creates a new instance of GSMConnect */
public GSMConnect(String comm) {</p>
<pre class="prettyprint">this.comPort = comm;
</code></pre>
<p> }</p>
<p> public boolean init() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("init");
return true;
}
}
}
return false;
}</p>
<p> public void checkStatus() {
send("AT+CREG?\r\n");
}</p>
<p> public void dial(String phoneNumber) {
try {
//dial to this phone number
messageString = "ATD" + phoneNumber + ";\n\r";
outputStream.write(messageString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}</p>
<p> public void send(String cmd) {
try {
outputStream.write(cmd.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}</p>
<p> public void sendMessage(String phoneNumber, String message) {
send("AT+CMGS=\"" + phoneNumber + "\"\r\n");
send(message + '\032');
}</p>
<p> public void hangup() {
send("ATH\r\n");
}</p>
<p> public void connect() throws NullPointerException {
if (portId != null) {
try {
portId.addPortOwnershipListener(this);
serialPort = (SerialPort) portId.open("MobileGateWay", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
}</p>
<pre class="prettyprint"> try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
/** These are the events we want to know about*/
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
</code></pre>
<p>//Register to home network of sim card</p>
<pre class="prettyprint"> send("ATZ\r\n");
} else {
throw new NullPointerException("COM Port not found!!");
}
</code></pre>
<p> }</p>
<p> public void serialEvent(javax.comm.SerialPortEvent serialPortEvent) {
switch (serialPortEvent.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<em>BUFFER</em>EMPTY:
case SerialPortEvent.DATA_AVAILABLE:</p>
<pre class="prettyprint"> byte[] readBuffer = new byte[2048];
try {
while (true)
{
int numBytes = inputStream.read(readBuffer);
}
</code></pre>
<p>//print response message
//System.out.print(new String(readBuffer));
} catch (IOException e) {
}
break;
}
}</p>
<p> public void ownershipChange(int type) {
switch (type) {
case CommPortOwnershipListener.PORT_UNOWNED:
System.out.println(portId.getName() + ": PORT_UNOWNED");
break;
case CommPortOwnershipListener.PORT_OWNED:
System.out.println(portId.getName() + ": PORT_OWNED");
break;
case CommPortOwnershipListener.PORT<em>OWNERSHIP</em>REQUESTED:
System.out.println(portId.getName() + ": PORT_INUSED");
break;
}</p>
<p> }</p>
<p> public static void main(String args[]) {
GSMConnect gsm = new GSMConnect("COM3");
if (gsm.init()) {
try {
gsm.connect();
gsm.checkStatus();
Thread.sleep(5000);
gsm.sendMessage("+9400530373", "hai dis is test");
//gsm.dial("+9400530373");
Thread.sleep(20000);
gsm.hangup();
// gsm.dial("+9400530373");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Can't init this card");
}
}
}</p>