Java調用SMS Cat(短信貓)發短信

這個是SMS Cat設備:

 

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

 

       插好USB和電源後,會安裝好驅動,可以在設備管理可以查看到端口號


 

也有一種軟件可以測試端口是否可連接:secureCRT


 

新建好connection後,輸入AT測試,如果OK就OK。


好了,端口確定沒問題了。

 

 

接着,在你的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. }  
 

測試結果自己找個手機號測吧


看見了嗎?控制檯中的服務(紅色標識)還在跑着,也就是端口還在佔用着;服務沒斷,可以不用重新初始化。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章