现在的位置: 首页 > 综合 > 正文

短信猫接收与发送短信整理 短信猫接收与发送短信整理

2018年05月23日 ⁄ 综合 ⁄ 共 8194字 ⁄ 字号 评论关闭
                                                            

短信猫接收与发送短信整理

一、主要就用到三个包:

1、log4j-1.2.16.jar
2、smslib-3.5.1.jar
3、comm.jar(这个不需要拷到lib下面)

二、在Windows环境下使用SMSLib编程的时候,我们需要做一下comm的配置:

1、将win32com.dll放置在%JAVA_HOME%\jre\bin下
2、将comm.jar放置在%JAVA_HOME%\jre\lib\ext下
3、将javax.comm.properties放置在%JAVA_HOME%\jre\lib下
上面三个文件可以去http://smslib.googlecode.com/files/javacomm20-win32.zip这里下载

然后下面是两个发送与接收短信的示例代码(经过一些修改)

SendMessage.java

[java] view
plain
copy

  1. package ob;  
  2.   
  3. import org.smslib.AGateway;  
  4. import org.smslib.GatewayException;  
  5. import org.smslib.IOutboundMessageNotification;  
  6. import org.smslib.OutboundMessage;  
  7. import org.smslib.Service;  
  8. import org.smslib.Message.MessageEncodings;  
  9. import org.smslib.modem.SerialModemGateway;  
  10.   
  11. public class SendMessage {  
  12.     public class OutboundNotification implements IOutboundMessageNotification {  
  13.         public void process(AGateway agateway, OutboundMessage outboundmessage) {  
  14.             System.out.println("Outbound handler called from Gateway: " + agateway);  
  15.             System.out.println(outboundmessage);  
  16.               
  17.         }  
  18.     }  
  19.   
  20.     @SuppressWarnings("deprecation")  
  21.     public void sendSMS(String mobilePhones, String content) throws GatewayException {  
  22.         Service srv;  
  23.         OutboundMessage msg;  
  24.         OutboundNotification outboundNotification = new OutboundNotification();  
  25. //      srv = new Service();  
  26.         srv = Service.getInstance();  
  27.         SerialModemGateway gateway = new SerialModemGateway("modem.com3""COM3"115200"wavecom"""); // 设置端口与波特率  
  28.         gateway.setInbound(true);  
  29.         gateway.setOutbound(true);  
  30.         gateway.setSimPin("1234");  
  31. //      gateway.setOutboundNotification(outboundNotification);  
  32.         srv.setOutboundMessageNotification(outboundNotification);  
  33.         srv.addGateway(gateway);  
  34.         System.out.println("初始化成功,准备开启服务");  
  35.         try {  
  36.             srv.startService();  
  37.             System.out.println("服务启动成功");  
  38.             String[] phones = mobilePhones.split(",");  
  39.             for (int i = 0; i < phones.length; i++) {  
  40.                 msg = new OutboundMessage(phones[i], content);  
  41.                 msg.setEncoding(MessageEncodings.ENCUCS2); // 中文  
  42.                 srv.sendMessage(msg);  
  43.             }  
  44.             srv.stopService();  
  45.                         srv.removeGateway(gateway);  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50.   
  51.     public static void main(String[] args) throws GatewayException {  
  52.         SendMessage sendMessage = new SendMessage();  
  53.         sendMessage.sendSMS("13808080808""短信内容");  
  54.     }  
  55. }  

ReadMessages.java

[java] view
plain
copy

  1. package ob;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import javax.crypto.spec.SecretKeySpec;  
  6. import org.smslib.AGateway;  
  7. import org.smslib.ICallNotification;  
  8. import org.smslib.IGatewayStatusNotification;  
  9. import org.smslib.IInboundMessageNotification;  
  10. import org.smslib.IOrphanedMessageNotification;  
  11. import org.smslib.InboundMessage;  
  12. import org.smslib.Library;  
  13. import org.smslib.Service;  
  14. import org.smslib.AGateway.GatewayStatuses;  
  15. import org.smslib.AGateway.Protocols;  
  16. import org.smslib.InboundMessage.MessageClasses;  
  17. import org.smslib.Message.MessageTypes;  
  18. import org.smslib.crypto.AESKey;  
  19. import org.smslib.modem.SerialModemGateway;  
  20.   
  21. public class ReadMessages {  
  22.     public static Service srv = Service.getInstance();  
  23.   
  24.     public void doIt() throws Exception {  
  25.         List<InboundMessage> msgList;  
  26.         InboundNotification inboundNotification = new InboundNotification();  
  27.         CallNotification callNotification = new CallNotification();  
  28.         GatewayStatusNotification statusNotification = new GatewayStatusNotification();  
  29.         OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();  
  30.         try {  
  31.             System.out.println("Example: Read messages from a serial gsm modem.");  
  32.             System.out.println(Library.getLibraryDescription());  
  33.             System.out.println("Version: " + Library.getLibraryVersion());  
  34.             SerialModemGateway gateway = new SerialModemGateway("modem.com3""COM3"115200nullnull);  
  35.             gateway.setProtocol(Protocols.PDU);  
  36.             gateway.setInbound(true);  
  37.             gateway.setOutbound(true);  
  38.             srv.setInboundMessageNotification(inboundNotification);  
  39.             srv.setCallNotification(callNotification);  
  40.             srv.setGatewayStatusNotification(statusNotification);  
  41.             srv.setOrphanedMessageNotification(orphanedMessageNotification);  
  42.             srv.addGateway(gateway);  
  43.             srv.startService();  
  44.             System.out.println();  
  45.             System.out.println("Modem Information:");  
  46.             System.out.println(" Manufacturer: " + gateway.getManufacturer());  
  47.             System.out.println(" Model: " + gateway.getModel());  
  48.             System.out.println(" Serial No: " + gateway.getSerialNo());  
  49.             System.out.println(" SIM IMSI: " + gateway.getImsi());  
  50.             System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%");  
  51.             System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");  
  52.             System.out.println();  
  53.             srv.getKeyManager().registerKey("+8613808080808"new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));  
  54.             msgList = new ArrayList<InboundMessage>();  
  55.             srv.readMessages(msgList, MessageClasses.ALL);  
  56.             for (InboundMessage msg : msgList) {  
  57.                 System.out.println(msg);  
  58. //              srv.deleteMessage(msg);     //删除短信  
  59.             }  
  60.             System.out.println("Now Sleeping - Hit <enter> to stop service.");  
  61.             System.in.read();  
  62.             System.in.read();  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.     }  
  67.   
  68.     public class InboundNotification implements IInboundMessageNotification {  
  69.         public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) {  
  70.             if (msgType == MessageTypes.INBOUND)  
  71.                 System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());  
  72.             else if (msgType == MessageTypes.STATUSREPORT)  
  73.                 System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());  
  74.             System.out.println(msg);  
  75.         }  
  76.     }  
  77.   
  78.     public class CallNotification implements ICallNotification {  
  79.         public void process(AGateway gateway, String callerId) {  
  80.             System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);  
  81.         }  
  82.     }  
  83.   
  84.     public class GatewayStatusNotification implements IGatewayStatusNotification {  
  85.         public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus) {  
  86.             System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);  
  87.         }  
  88.     }  
  89.   
  90.     public class OrphanedMessageNotification implements IOrphanedMessageNotification {  
  91.         public boolean process(AGateway gateway, InboundMessage msg) {  
  92.             System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());  
  93.             System.out.println(msg);  
  94.             return false;  
  95.         }  
  96.     }  
  97.   
  98.     public static void main(String args[]) {  
  99.         ReadMessages app = new ReadMessages();  
  100.         try {  
  101.             app.doIt();  
  102.         } catch (Exception e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.     }  
  106. }  

上面代码在第一次运行都是正常的,有个问题是,用的是usb接口的短信猫,好像是停止不了服务的,第二次接收或者发送,总会报一个错:

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by org.smslib
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
    at org.smslib.Service$1Starter.run(Service.java:276)

找了很多资料,都没找到解决方法。估计只有串口的短信猫设备才能正常。。

目前是把Service一直开启,不做停止,然后一直重复读取短信,弄了个循环,临时解决。

会报错是因为没有移除端口,在srv.stopService();后面加一句srv.removeGateway(gateway);即可。


抱歉!评论已关闭.