1.[文件]
SmSMassage.java ~ 7KB
下载(89)
import org.smslib.Message;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.helper.CommPortIdentifier;
import org.smslib.helper.SerialPort;
import org.smslib.modem.SerialModemGateway;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
/**
* ------------------------------------------------
*
Copyright (C) All rights reserved by KK
*
* Developer: ZhouHong
* Date: 15-4-16 下午1:38
*
* Change Description
*
* ------------------------------------------------
*/
public class SmSMassage {
//获取COM口,注释放开即读配置文件中配置的COM串口
public String getPort() {
return "COM8";
// String courseFile = this.getClass().getResource("/smsPhone.properties").getPath();
// Properties prop = new Properties();
// try {
// prop.load(new FileInputStream(courseFile));
// return prop.getProperty("smsPort").equals("")?"COM1":prop.getProperty("smsPort");
// } catch (IOException e) {
// return "COM1";
// }
}
public static void main(String[] args) {
SmSMassage sos = new SmSMassage();
sos.SMSMessage(new String[]{"185xxxxxxxx"}, "测试短信");
try {
sos.SOSIphone("18516071901");
} catch (PortInUseException e) {
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (TooManyListenersException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// smsIphone(port); //放入单独线程中运行
// smsMessage(map);//放入单独线程中运行
}
public void smsIphone(String telephone){
final String phone = telephone;
new Thread(){
@Override
public void run() {
try {
SOSIphone(phone);
} catch (PortInUseException e1) {
e1.printStackTrace();
} catch (UnsupportedCommOperationException e1) {
e1.printStackTrace();
} catch (TooManyListenersException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}.start();
}
public void smsMessage(String[] telephones, String msg){
final String[] iphones = telephones;
final String text = msg;
new Thread(){
@Override
public void run() {
SMSMessage(iphones, text);
}
}.start();
}
private boolean checkPort(String port){
boolean flag = false;
for (Enumeration e = CommPortIdentifier
.getPortIdentifiers(); e.hasMoreElements();) {
CommPortIdentifier portId = e.nextElement();
if (portId.getName().equals(port)) {
flag = true;
break;
}
}
return flag;
}
private void SMSMessage(String[] phones,String text){
String port = new SmSMassage().getPort();
CommPortIdentifier portId = getPortP(port);
// SerialPort serialPort = null;
try{
if (portId!=null) {
if (phones.length<=0)
return;
// serialPort = portId.open("wavecom1", 1000);
// Service srv = Service.getInstance();
SerialModemGateway gateway = new SerialModemGateway("modem.com5",
port, 115200, "WAVECOM", "–");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
List outboundMessageList = new ArrayList();
for (String phone:phones){
OutboundMessage msg = new OutboundMessage(phone,text);
msg.setEncoding(Message.MessageEncodings.ENCUCS2);
outboundMessageList.add(msg);
}
Service.getInstance().sendMessages(outboundMessageList);
// for (AGateway g: srv.getGateways()) {
// g.stopGateway();
// }
gateway.stopGateway();
Service.getInstance().stopService();
Service.getInstance().removeGateway(gateway);
System.out.print("短信发送成功!");
}else{
System.out.print("找不到端口"+port);
}
}catch(Exception e){
e.getMessage();
}
}
private void SOSIphone(String telephone)throws PortInUseException, UnsupportedCommOperationException,
TooManyListenersException, IOException, InterruptedException{
String port = new SmSMassage().getPort();
CommPortIdentifier portId = getPortP(port);
if (portId!=null) {
final SerialPort serialPort = portId.open("wavecom1", 1000);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
serialPort.setSerialPortParams(115200,// 波特率
SerialPort.DATABITS_8,// 数据位数
SerialPort.STOPBITS_1, // 停止位
SerialPort.PARITY_NONE);// 奇偶位
System.out.println("端口已打开。\n发送AT指令 …");
InputStream inputStream = serialPort.getInputStream();
OutputStream outputStream = serialPort.getOutputStream();
String at_cmd = "ATD"+telephone.trim()+";\r";
outputStream.write(at_cmd.getBytes());
Thread.sleep(2000);
byte[] data = new byte[1024];
inputStream.close();
outputStream.close();
serialPort.close();
System.out.println("关闭端口。");
}
}
//获取端口是否存在及端口状态
private static CommPortIdentifier getPortP(String port){
CommPortIdentifier comm = null;
for (Enumeration e = CommPortIdentifier
.getPortIdentifiers(); e.hasMoreElements();) {
CommPortIdentifier portId = e.nextElement();
if (portId.getName().equals(port)) {
comm = portId;
break;
}
}
return comm;
}
}