短信设备GSM MODEM使用smslib二次开发包读取短信例子
分享只要是GSM modem都使用smslib这个短信二次开发包,他封装了标准指令,现在所有短信模块都遵循标准AT指令后再延伸其他指令出来的,收发短信指令最大多数模块都可以使用,各别有个性的模块生产厂商除外。同时也适合4G LTE MODEM和3G MODEM的。注意国内CDMA制式的留意,这个SMSLIB开发包仅支持英文短信,中文支持,如需要支持需要自己修改添加对应的U码转换中文的方式。如需要3gpp2的PDU编解码这个还需要你自己去官网找这个协议去了解添加,说明比较复杂国内这方面资料不多,可以自己尝试国外找找看,国外很少用CDMA的制式所以比较难找。
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(); } } }
回帖 ( 0 )