Skip to content

Instantly share code, notes, and snippets.

@dagvadorj
Created May 1, 2013 17:52
Show Gist options
  • Save dagvadorj/5496913 to your computer and use it in GitHub Desktop.
Save dagvadorj/5496913 to your computer and use it in GitHub Desktop.
SMS
// Send confirmation SMS
// Uses javax comm
// Installing guide found at http://smslib.org/doc/installation/
// File comm.jar should go under JDKDIR/jre/lib/ext/
// File javax.comm.properties should go under JDKDIR/jre/lib/
// Library files (i.e. win32com.dll for Win32 or the .so Linux library files) should go under JDKDIR/jre/bin/
System.out.println("Ports...");
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("hasElements: " + portList.hasMoreElements());
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
System.out.println("Port name: " + portId.getName());
System.out.println("Port type: " + portId.getPortType());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL && portId.getName().equals("COM6")) {
System.out.println("Using port...");
String textMessage = "";
textMessage = textMessage.concat("AT").concat(System.getProperty("line.separator"));
textMessage = textMessage.concat("AT+CMGF=1").concat(System.getProperty("line.separator"));
textMessage =
textMessage.concat("AT+CMGS=\"+976XXXXXXXX\"").concat(System.getProperty("line.separator"));
textMessage = textMessage.concat("Hello, world!");
System.out.println("Sending...");
System.out.println(textMessage);
SerialPort serialPort;
OutputStream outputStream;
try {
serialPort = (SerialPort)portId.open("SMS", 1000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
outputStream = serialPort.getOutputStream();
} catch (PortInUseException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
return;
}
try {
outputStream.write(textMessage.getBytes());
outputStream.flush();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment