????? 最近公司项目需要使用短信
设备进行相关操作提示,于是申请了一个西门子模块的短信设备。今天刚刚到手就开始测试,按照淘宝卖家的说明进行操作总是报
异常。
org.smslib.GatewayException: Comm library exception: java.lang.reflect.Invocatio
nTargetException
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java
:93)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:106)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)
at org.smslib.Service$1Starter.run(Service.java:227)</
询问卖家原因可能在什么地方,得到的回答是不懂开发。于是开始自己“埋头苦干”。首先确定了驱动没有问题,USB模拟串口没有问题,基础环境和jar包等路径没有问题。-_-!我就崩溃了,到底是哪里的问题呢,分析了一圈,
发现Myeclipse中“windows”->“preferences”->"java"->"installed jres",Myeclipse默认使用的是自己的JRE,需要手动将你选择你安装的“JAVA_HOME”/JRE,这样问题就解决了。在使用卖家提供的smslib-3.3.0b2.jar只能使用网上的
发送短信源码,不能使用接收短信源码。请将jar文件替换成smslib-3.5.1.jar就可以了
现将网上的源码公布一下:
测试端口
package test;
import java.util.Enumeration;
import org.smslib.helper.CommPortIdentifier;
public class TEST {
public static void main(String [] args){
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
System.out.println("=============");
}
}
}
}
?发送短信源码
package test;
import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;
public class SendMessage {
public class OutboundNotification implements IOutboundMessageNotification {
public void process(AGateway agateway, OutboundMessage outboundmessage) {
System.out.println("Outbound handler called from Gateway: " + agateway);
System.out.println(outboundmessage);
}
}
@SuppressWarnings("deprecation")
public void sendSMS(String mobilePhones, String content) throws GatewayException {
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
// srv = new Service();
srv = Service.getInstance();
SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "wavecom", ""); // 设置端口与波特率
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("1234");
// gateway.setOutboundNotification(outboundNotification);
srv.setOutboundMessageNotification(outboundNotification);
srv.addGateway(gateway);
System.out.println("初始化成功,准备开启服务");
try {
srv.startService();
System.out.println("服务启动成功");
String[] phones = mobilePhones.split(",");
for (int i = 0; i < phones.length; i++) {
msg = new OutboundMessage(phones[i], content);
msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
srv.sendMessage(msg);
}
srv.stopService();
srv.removeGateway(gateway);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws GatewayException {
SendMessage sendMessage = new SendMessage();
sendMessage.sendSMS("手机号码", "短信内容");
}
}
?接收短信源码
package test;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.spec.SecretKeySpec;
import org.smslib.AGateway;
import org.smslib.ICallNotification;
import org.smslib.IGatewayStatusNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOrphanedMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.Library;
import org.smslib.Service;
import org.smslib.AGateway.GatewayStatuses;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Message.MessageTypes;
import org.smslib.crypto.AESKey;
import org.smslib.modem.SerialModemGateway;
public class ReadMessages {
public static Service srv = Service.getInstance();
public void doIt() throws Exception {
List<InboundMessage> msgList;
InboundNotification inboundNotification = new InboundNotification();
CallNotification callNotification = new CallNotification();
GatewayStatusNotification statusNotification = new GatewayStatusNotification();
OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();
try {
System.out.println("Example: Read messages from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Version: " + Library.getLibraryVersion());
SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, null, null);
gateway.setProtocol(Protocols.PDU);
gateway.setInbound(true);
gateway.setOutbound(true);
srv.setInboundMessageNotification(inboundNotification);
srv.setCallNotification(callNotification);
srv.setGatewayStatusNotification(statusNotification);
srv.setOrphanedMessageNotification(orphanedMessageNotification);
srv.addGateway(gateway);
srv.startService();
System.out.println();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
System.out.println(" Model: " + gateway.getModel());
System.out.println(" Serial No: " + gateway.getSerialNo());
System.out.println(" SIM IMSI: " + gateway.getImsi());
System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%");
System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
System.out.println();
srv.getKeyManager().registerKey("+8613808080808", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));
msgList = new ArrayList<InboundMessage>();
srv.readMessages(msgList, MessageClasses.ALL);
for (InboundMessage msg : msgList) {
System.out.println(msg);
// srv.deleteMessage(msg); //删除短信
}
System.out.println("Now Sleeping - Hit <enter> to stop service.");
System.in.read();
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public class InboundNotification implements IInboundMessageNotification {
public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) {
if (msgType == MessageTypes.INBOUND)
System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());
else if (msgType == MessageTypes.STATUSREPORT)
System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
System.out.println(msg);
}
}
public class CallNotification implements ICallNotification {
public void process(AGateway gateway, String callerId) {
System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);
}
}
public class GatewayStatusNotification implements IGatewayStatusNotification {
public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus) {
System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
}
}
public class OrphanedMessageNotification implements IOrphanedMessageNotification {
public boolean process(AGateway gateway, InboundMessage msg) {
System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());
System.out.println(msg);
return false;
}
}
public static void main(String args[]) {
ReadMessages app = new ReadMessages();
try {
app.doIt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
?以上所有代码均经过XP测试通过,明天进行win03测试,后续准备进行linux的测试。