Java调用SMS Cat 短信猫 发短信

                这个是SMS Cat设备:

 

      需要插入SIM卡,所以是需要付短信费的。

 

       插好USB和电源后,会安装好驱动,可以在设备管理可以查看到端口号


 

也有一种软件可以测试端口是否可连接:secureCRT


 

新建好connection后,输入AT测试,如果OK就OK。


扫描二维码关注公众号,回复: 5970693 查看本文章

好了,端口确定没问题了。

 

 

接着,在你的JDK的bin路径下放一个win32com.dll


 

在JDK的lib中放一个comm.jar和javax.comm.properties


 

需要的文件都在附件SMSCat.rar中。

 

 

OK,所有都准备完了,现在开始代码测试。

 

 

demo测试需要用到的jar


 

 

sms.properties#sms properties

Java代码   收藏代码
  1. Message.comId=modem.com19  
  2. Message.com=COM19  
  3. Message.baudRate=9600  
  4. Message.manufacturer=wavecom  
  5. Message.model=  
  6. Message.simPin=0000  
 

注意:这里的端口COM19必须和你上面的端口对应

 

SMSService.java

 

Java代码   收藏代码
  1. /** 
  2.  * Copyright(C) 2012 GZ ISCAS ALL Rights Reserved 
  3.  */  
  4. package com.royal.SMSCat;  
  5.   
  6. import java.util.Properties;  
  7.   
  8. import org.smslib.Message.MessageEncodings;  
  9. import org.smslib.OutboundMessage;  
  10. import org.smslib.Service;  
  11. import org.smslib.modem.SerialModemGateway;  
  12.   
  13. import com.royal.utils.PropertiesUtil;  
  14.   
  15. /** 
  16.  * 描述:SMS Cat服务类 
  17.  */  
  18. public class SMSService {  
  19.   
  20.     /** 
  21.      * 私有静态实例 
  22.      */  
  23.     private static SMSService instance = null;  
  24.   
  25.     /** 
  26.      * 是否开启服务 
  27.      */  
  28.     private boolean isStartService = false;  
  29.   
  30.     /** 
  31.      * 私有构造方法 
  32.      */  
  33.     private SMSService() {  
  34.     }  
  35.   
  36.     /** 
  37.      * 获取实例(单例模式) 
  38.      *  
  39.      * @return 
  40.      */  
  41.     public static SMSService getInstance() {  
  42.         if (instance == null) {  
  43.             instance = new SMSService();  
  44.         }  
  45.         return instance;  
  46.     }  
  47.   
  48.     /** 
  49.      * 开启短信服务 
  50.      *  
  51.      * @param path 
  52.      *            配置文件路径 
  53.      */  
  54.     public void startService(String path) {  
  55.         System.out.println("开始初始化SMS服务!");  
  56.   
  57.         // 加载文件属性  
  58.         Properties p = null;  
  59.         try {  
  60.             p = PropertiesUtil.getProperties(path);  
  61.         } catch (Exception e) {  
  62.             System.out.println("加载属性文件出错:" + e.getMessage());  
  63.             return;  
  64.         }  
  65.   
  66.         // 初始化网关,参数信息依次为:COMID,COM号,比特率,制造商,Modem模式  
  67.         SerialModemGateway gateway = new SerialModemGateway(p.getProperty("Message.comId"), p.getProperty("Message.com"), Integer.parseInt(p.getProperty("Message.baudRate")), p.getProperty("Message.manufacturer"), p.getProperty("Message.model"));  
  68.   
  69.         gateway.setInbound(true);  
  70.         gateway.setOutbound(true);  
  71.         gateway.setSimPin(p.getProperty("Message.simPin"));  
  72.   
  73.         OutboundNotification outboundNotification = new OutboundNotification();  
  74.   
  75.         Service service = Service.getInstance();  
  76.         if (service == null) {  
  77.             System.out.println("初始化SMS服务失败!");  
  78.             return;  
  79.         }  
  80.   
  81.         service.setOutboundMessageNotification(outboundNotification);  
  82.         try {  
  83.             service.addGateway(gateway);  
  84.             // 开启服务  
  85.             service.startService();  
  86.             System.out.println("初始化SMS服务成功!");  
  87.             isStartService = true;  
  88.         } catch (Exception e) {  
  89.             System.out.println("开启SMS服务异常:" + e.getMessage());  
  90.         }   
  91.     }  
  92.   
  93.     /** 
  94.      * 停止SMS服务 
  95.      */  
  96.     public void stopService() {  
  97.         try {  
  98.             Service.getInstance().stopService();  
  99.         } catch (Exception e) {  
  100.             System.out.println("关闭SMS服务异常:" + e.getMessage());  
  101.         }   
  102.         isStartService = false;  
  103.     }  
  104.   
  105.     /** 
  106.      * 发送短信 
  107.      *  
  108.      * @param toNumber 
  109.      *            手机号码 
  110.      * @param message 
  111.      *            短信内容 
  112.      */  
  113.     public void sendMessage(String toNumber, String message) {  
  114.         if (!isStartService) {  
  115.             System.out.println("尚未开启SMS服务!");  
  116.             return;  
  117.         }  
  118.   
  119.         // 封装信息  
  120.         OutboundMessage msg = new OutboundMessage(toNumber, message);  
  121.         msg.setEncoding(MessageEncodings.ENCUCS2);  
  122.         try {  
  123.             // 发送信息  
  124.             Service.getInstance().sendMessage(msg);  
  125.         } catch (Exception e) {  
  126.             System.out.println("SMS服务发送信息发生异常:" + e.getMessage());  
  127.             isStartService = false;  
  128.         }   
  129.     }  
  130.   
  131. }  
 

OutboundNotification.java

 

Java代码   收藏代码
  1. package com.royal.SMSCat;  
  2.   
  3. import org.smslib.AGateway;  
  4. import org.smslib.IOutboundMessageNotification;  
  5. import org.smslib.OutboundMessage;  
  6.   
  7. /** 
  8.  * 封装发送短信类 
  9.  */  
  10. public class OutboundNotification implements IOutboundMessageNotification {  
  11.       
  12.     public void process(AGateway gateway, OutboundMessage msg) {  
  13.         System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());  
  14.     }  
  15. }  
 

SMSCatClient.java

 

Java代码   收藏代码
  1. package com.royal.SMSCat;  
  2.   
  3. public class SMSCatClient {  
  4.       
  5.     /** 
  6.      * 测试 
  7.      *  
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         String path = "D:\\sms.properties";  
  12.         SMSService.getInstance().startService(path);  
  13.         SMSService.getInstance().sendMessage("13800138000""测试 Test!");  
  14.         //没必要的时候没停止服务,因为端口占用着  
  15.         SMSService.getInstance().stopService();  
  16.     }  
  17.   
  18. }  
 

测试结果自己找个手机号测吧


看见了吗?控制台中的服务(红色标识)还在跑着,也就是端口还在占用着;服务没断,可以不用重新初始化。

 

           

猜你喜欢

转载自blog.csdn.net/qq_44894516/article/details/89417649