短信猫简单配置:https://www.cnblogs.com/Big-Boss/p/9699880.html
测试发送短信代码:https://www.cnblogs.com/Big-Boss/p/9699960.html
读取短信:
package utils; import java.util.ArrayList; import java.util.List; import org.smslib.AGateway; import org.smslib.AGateway.GatewayStatuses; import org.smslib.AGateway.Protocols; import org.smslib.ICallNotification; import org.smslib.IGatewayStatusNotification; import org.smslib.IInboundMessageNotification; import org.smslib.IOrphanedMessageNotification; import org.smslib.InboundMessage; import org.smslib.InboundMessage.MessageClasses; import org.smslib.Message.MessageTypes; import org.smslib.Service; import org.smslib.modem.SerialModemGateway; /** * 读取短信 * @author 【】 * */ public class ReadMessagesUtil { 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(); // ---------------创建串口设备,如果有多个,就创建多个-------------- // 1、连接网关的id // 2、com口名称,如COM1或/dev/ttyS1(根据实际情况修改) // 3、串口波特率,如9600(根据实际情况修改) // 4、开发商 // 5、型号 SerialModemGateway gateway = new SerialModemGateway("modem.com6", "COM6", 9600, "null", "null"); gateway.setProtocol(Protocols.PDU); // 设置true,表示该网关可以接收短信,根据需求修改 gateway.setInbound(true); // 设置true,表示该网关可以发送短信,根据需求修改 gateway.setOutbound(true); // sim卡锁,一般默认为0000或1234 gateway.setSimPin("1234"); try { // 接收到新短信时回调 srv.setInboundMessageNotification(inboundNotification); // 接收到电话时回调 srv.setCallNotification(callNotification); // gateway状态发生改变时回调 srv.setGatewayStatusNotification(statusNotification); // 监测到孤儿短信(不完整短信)存在时回调 srv.setOrphanedMessageNotification(orphanedMessageNotification); // 将网关添加到短信猫服务中 srv.addGateway(gateway); // 启动服务 srv.startService(); System.out.println("---------------------------服务启动完毕----------------------------"); // 设置加密 //srv.getKeyManager().registerKey("短信中心号码", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES"))); msgList = new ArrayList<InboundMessage>(); // 读取短信 srv.readMessages(msgList, MessageClasses.ALL); for (InboundMessage msg : msgList) { System.out.println(msg.getMemIndex()); System.out.println("发信人:" + msg.getOriginator()); System.out.println("短信内容:" + msg.getText()); /*System.out.println("-----------------开始删除----------------"); srv.deleteMessage(msg); //删除短信 System.out.println("-----------------删除完毕----------------");*/ } System.out.println("Now Sleeping - Hit <enter> to stop service."); System.in.read(); System.in.read(); } catch (Exception e) { e.printStackTrace(); } finally { srv.stopService(); srv.removeGateway(gateway); } } 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[]) { ReadMessagesUtil app = new ReadMessagesUtil(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }
读取到的短信信息: