Java Reference
In-Depth Information
CommPortIdentifier portId;
// bind to the first serial port attached to a GSM enabled
// modem.
public
SMSSender() throws Exception {
super();
portId
#
getMobilePort();
if
(portId!
#
null
)
System.out.println("GSM equipment found: "
!
portId.getName());
else
throw new
Exception("No mobile modem available");
}
public void
finalize(){
close();
}
// Open the connection to the mobile.
public void
open()
throws
Exception {
serialPort
#
(SerialPort) portId.open
("SMSSender", 2000);
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
inputStream
#
serialPort.getInputStream();
outputStream
#
serialPort.getOutputStream();
serialPort.addEventListener(
this
);
serialPort.notifyOnDataAvailable(
true
);
}
// Close the connection to the mobile.
public
void close(){
try
{
if
(serialPort !
#
null
){
serialPort.removeEventListener();
inputStream.close();
outputStream.close();
serialPort.close();
}
}
catch
(Exception e){}
serialPort
#
null
;
}
// Look for a serial port attached to a GSM-enable device
CommPortIdentifier getMobilePort()
throws
Exception {
Enumeration portList
#
CommPortIdentifier.
getPortIdentifiers();
while
(portList.hasMoreElements()) {
portId
#
(CommPortIdentifier) portList.nextElement();