1, 拷贝底层串口依赖的librxtxSerial.so到JDK安装路径
cp librxtxSerial.so /usr/java/{0}/jre/lib/{1}
# {0}: JDK的基础目录,例如: jdk1.7.0_55; {1}: 处理器名称,例如: amd64,i386
2, 在工程中添加RXTXcomm.jar, smslib-3.5.1.jar
librxtxSerial.so和RXTXcomm.jar下载地址:http://rxtx.qbang.org
smslib-3.5.1.jar可以从Maven中下载依赖
3, 测试code
package com.smsmanager.singleInstall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;
import com.common.constant.SMSProperties;
public class ServiceFactory
{
private static Logger logger = LoggerFactory.getLogger(ServiceFactory.class);
/**
* 创建串口设备,如果有多个,就创建多个 参数说明:
* 1、你自己连接网关的id,名称好像是没有约束,自己随便定义
* 2、com口名称,如COM1或/dev/ttyS1(根据实际情况修改),可使用ComTone.java程序获取
* 3、串口波特率,如9600(根据实际情况修改),可使用ComTone.java程序获取
* 4、短信猫厂商,如Apple,可置为空
* 5、短信猫型号,如iphone4s,可置为空
*/
private static SerialModemGateway gateway;
/**
* 启动的发送短信Service,设为静态变量,打开服务后就不关闭,实现连续发短信
*/
private static Service srv;
static
{
init();
}
public static void init()
{
// 设置端口与波特率
gateway = new SerialModemGateway(SMSProperties.SERIALMODEMGATEWAY_ID, SMSProperties.SERIALMODEMGATEWAY_COMPORT,
SMSProperties.SERIALMODEMGATEWAY_BAUDRATE, SMSProperties.SERIALMODEMGATEWAY_MANUFACTURER, SMSProperties.SERIALMODEMGATEWAY_MODEL);
gateway.setInbound(true);
gateway.setOutbound(true);
srv = Service.getInstance();
srv.setOutboundMessageNotification(new OutboundNotification());
try
{
srv.addGateway(gateway);
// 启用轮循模式
srv.S.SERIAL_POLLING = true;
logger.info("短信猫准备开启服务.");
srv.startService();
logger.info("短信猫服务启动成功.");
}
catch (Exception e)
{
logger.error("短信猫启动失败,原因:", e);
}
}
public static class OutboundNotification implements IOutboundMessageNotification
{
public void process(AGateway gateway, OutboundMessage msg)
{
logger.debug(msg.getText() + "已发送。");
}
}
/**
* 给特定短信号码数组发送短信
*
* @param phones 短信号码数组
* @param content 发送短信内容
*/
public static boolean sendSmsInfo(String phone, String content)
{
boolean isSendSuc = false;
if (null == srv)
{
return isSendSuc;
}
try
{
OutboundMessage msg;
msg = new OutboundMessage(phone, content);
// 中文
msg.setEncoding(MessageEncodings.ENCUCS2);
isSendSuc = srv.sendMessage(msg);
}
catch (Exception e)
{
logger.info(content + "发送失败,具体原因:", e);
}
return isSendSuc;
}
public static boolean stopService()
{
boolean result = false;
try
{
srv.stopService();// 关闭服务
srv.removeGateway(gateway);
srv = null;
gateway = null;
result = true;
}
catch (Exception e)
{
logger.error("短信猫关闭失败,原因:", e);
}
return result;
}
public static void restartService()
{
stopService();
init();
}
}
4,FAQ
4.1,异常:org.smslib.GatewayException: GSM Network Auto-Registration disabled!
解释: 无法正常链接gsm网络
原因1:SIM卡没插好
解决1:请重新插卡
原因2:SIM卡没钱了
解决2:请查看账户余额,或打电话试试
原因3:周围的网络信号不强,导致SIM卡无法连接到网络
解决3:请使用自己的手机进行移动信号强度测试
4.2,异常:org.smslib.GatewayException: GSM: Invalid CREG response.
at org.smslib.modem.AModemDriver.waitForNetworkRegistration(AModemDriver.java:459)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:165)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:186)
at org.smslib.Service$1Starter.run(Service.java:257)
1,是 sim 卡出问题了。GSM: Invalid CREG response. 卡得不到响应。
4.3,异常:org.smslib.TimeoutException: No response from device.
at org.smslib.modem.AModemDriver$CharQueue.get(AModemDriver.java:531)
at org.smslib.modem.AModemDriver.getResponse(AModemDriver.java:329)
at org.smslib.modem.athandler.ATHandler.getSimStatus(ATHandler.java:132)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:129)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:186)
at org.smslib.Service$1Starter.run(Service.java:257)
解释:不可以长时间的占用端口,在一定时间之后应该及时的释放端口和资源
原因1:端口可能未正常打开
解决1:请重新打开端口
原因2:可能使用远程连接,选项中的 本地资源中端口被全部勾上了
解决2:请去除本地资源的勾选