rxtx+smslib|rxtx+smslib java控制短信猫


由于项目需要,使用了短信猫,在群里问了好多人,都说这个过时了,后来在网上找了下短信接口公司,又怕服务不好,只好硬着头皮,折腾了一个星期,终于稳定了。

我买的短信猫比较便宜,所以没有开发支持,只有开源包,由于smslib的开源包在64位系统下不能使用,所以添加了rxtx控制窜口。rxtx包可网上下载(http://download.csdn.net/detail/chenjianghui1234/9805235)。smslib包下载(http://download.csdn.net/detail/chenjianghui1234/9805235)

1:将压缩包下的rxtxParallel.dll和rxtxSerial.dll放到%JAVA_HOME%(如C:\jdk1.7.0_80\jre\bin)的bin目录下(或者将两个dll文件导入eclipse的lib下)。
2:将RXTXcomm.jar放到%JAVA_HOME%的(如:C:\jdk1.7.0_80\jre\lib\ext)ext目录下。
3:将smslib的jar包导入工程。
4:调用开源包发送短信。
5:第一次发送短信后,不可将服务断开,否则会无法发送,可将服务一直开启,随tomcat的一同关闭。以下是代码;
package com.will.util;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.smslib.GatewayException;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;
import org.smslib.Message.MessageEncodings;

public class MsgSendThread {
private static final Service service = SendManage.doIt(); ;

public static long lastSent = 0;

public static void startService() {
final Service service = SendManage.doIt();
}

public static void stopService() {
try {
service.stopService();
service.removeGateway(SendManage.getGateway());
System.out.println("已停止服务,正在重启中..........");
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static boolean doSend(String phone, String sendContent) {
OutboundMessage msg = null;
boolean flag = false;
msg = new OutboundMessage(phone, sendContent); // 参数1:手机号码
msg.setEncoding(MessageEncodings.ENCUCS2); // 这句话是发中文短信必须的
try {
long diff = System.currentTimeMillis() - lastSent;
if (diff < 5000) {
Thread.sleep(5000 - diff);
}
if (null != msg) {
flag = service.sendMessage(msg); // 执行发送短信
System.out.println("###########################################");
if (flag) {
System.out.println("发送短信成功!");
} else {
System.out.println("发送短信失败!");
}
System.out.println("*******************************************");
}

lastSent = System.currentTimeMillis();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (GatewayException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return flag;
}


}


package com.will.util;

import java.io.IOException;
import java.util.List;

import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;

public class SendManage {

private static final SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM4", 19200, "Siemens",
"");

public static Service doIt() {
Service service;
OutboundNotification outboundNotification = new OutboundNotification();
service = Service.getInstance();
try {
//service.S.SERIAL_POLLING_INTERVAL = 200;
service.setOutboundMessageNotification(outboundNotification); // 发送短信成功后的回调函方法
service.addGateway(getGateway());
service.S.SERIAL_POLLING = true; // 启用轮循模式
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 将网关添加到短信猫服务中
catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println("服务启动中...........");
// 启动服务,进入短信发送就绪状态
service.startService();
System.out.println("服务启动完成!");
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return service;
}



public static SerialModemGateway getGateway() {

// modem.com1:网关ID(即短信猫端口编号)
//COM4:串口名称(在window中以COMXX表示端口名称,在linux,unix平台下以ttyS0-N或ttyUSB0-N表示端口名称
//),通过端口检测程序得到可用的端口
//115200:串口每秒发送数据的bit位数,必须设置正确才可以正常发送短信,可通过程序进行检测。常用的有115200、9600
//Huawei:短信猫生产厂商,不同的短信猫生产厂商smslib所封装的AT指令接口会不一致,必须设置正确.常见的有Huawei、
//wavecom等厂商 最后一个参数表示设备的型号,可选

// SerialModemGateway gateway = new SerialModemGateway("modem.com3",
// "COM3", 19200, "wavecom", "");
gateway.setProtocol(Protocols.PDU);
gateway.setInbound(true); // 设置true,表示该网关可以接收短信,根据需求修改
gateway.setOutbound(true); // 设置true,表示该网关可以发送短信,根据需求修改
gateway.setSimPin("0000"); // sim卡锁,一般默认为0000或1234
//gateway.setInbound(true); // 设置网关可以写入信息
//gateway.setOutbound(true); // 设置网关可以读取信息
// gateway.setSmscNumber("+306942190000"); //短信服务中心号码
return gateway;

}

}



【rxtx+smslib|rxtx+smslib java控制短信猫】

    推荐阅读