需要插入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
- Message.comId=modem.com19
- Message.com=COM19
- Message.baudRate=9600
- Message.manufacturer=wavecom
- Message.model=
- Message.simPin=0000
注意:這裏的端口COM19必須和你上面的端口對應。
SMSService.java
- /**
- * Copyright(C) 2012 GZ ISCAS ALL Rights Reserved
- */
- package com.royal.SMSCat;
- import java.util.Properties;
- import org.smslib.Message.MessageEncodings;
- import org.smslib.OutboundMessage;
- import org.smslib.Service;
- import org.smslib.modem.SerialModemGateway;
- import com.royal.utils.PropertiesUtil;
- /**
- * 描述:SMS Cat服務類
- */
- public class SMSService {
- /**
- * 私有靜態實例
- */
- private static SMSService instance = null;
- /**
- * 是否開啓服務
- */
- private boolean isStartService = false;
- /**
- * 私有構造方法
- */
- private SMSService() {
- }
- /**
- * 獲取實例(單例模式)
- *
- * @return
- */
- public static SMSService getInstance() {
- if (instance == null) {
- instance = new SMSService();
- }
- return instance;
- }
- /**
- * 開啓短信服務
- *
- * @param path
- * 配置文件路徑
- */
- public void startService(String path) {
- System.out.println("開始初始化SMS服務!");
- // 加載文件屬性
- Properties p = null;
- try {
- p = PropertiesUtil.getProperties(path);
- } catch (Exception e) {
- System.out.println("加載屬性文件出錯:" + e.getMessage());
- return;
- }
- // 初始化網關,參數信息依次爲:COMID,COM號,比特率,製造商,Modem模式
- 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"));
- gateway.setInbound(true);
- gateway.setOutbound(true);
- gateway.setSimPin(p.getProperty("Message.simPin"));
- OutboundNotification outboundNotification = new OutboundNotification();
- Service service = Service.getInstance();
- if (service == null) {
- System.out.println("初始化SMS服務失敗!");
- return;
- }
- service.setOutboundMessageNotification(outboundNotification);
- try {
- service.addGateway(gateway);
- // 開啓服務
- service.startService();
- System.out.println("初始化SMS服務成功!");
- isStartService = true;
- } catch (Exception e) {
- System.out.println("開啓SMS服務異常:" + e.getMessage());
- }
- }
- /**
- * 停止SMS服務
- */
- public void stopService() {
- try {
- Service.getInstance().stopService();
- } catch (Exception e) {
- System.out.println("關閉SMS服務異常:" + e.getMessage());
- }
- isStartService = false;
- }
- /**
- * 發送短信
- *
- * @param toNumber
- * 手機號碼
- * @param message
- * 短信內容
- */
- public void sendMessage(String toNumber, String message) {
- if (!isStartService) {
- System.out.println("尚未開啓SMS服務!");
- return;
- }
- // 封裝信息
- OutboundMessage msg = new OutboundMessage(toNumber, message);
- msg.setEncoding(MessageEncodings.ENCUCS2);
- try {
- // 發送信息
- Service.getInstance().sendMessage(msg);
- } catch (Exception e) {
- System.out.println("SMS服務發送信息發生異常:" + e.getMessage());
- isStartService = false;
- }
- }
- }
OutboundNotification.java
- package com.royal.SMSCat;
- import org.smslib.AGateway;
- import org.smslib.IOutboundMessageNotification;
- import org.smslib.OutboundMessage;
- /**
- * 封裝發送短信類
- */
- public class OutboundNotification implements IOutboundMessageNotification {
- public void process(AGateway gateway, OutboundMessage msg) {
- System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());
- }
- }
SMSCatClient.java
- package com.royal.SMSCat;
- public class SMSCatClient {
- /**
- * 測試
- *
- * @param args
- */
- public static void main(String[] args) {
- String path = "D:\\sms.properties";
- SMSService.getInstance().startService(path);
- SMSService.getInstance().sendMessage("13800138000", "測試 Test!");
- //沒必要的時候沒停止服務,因爲端口占用着
- SMSService.getInstance().stopService();
- }
- }
測試結果自己找個手機號測吧
看見了嗎?控制檯中的服務(紅色標識)還在跑着,也就是端口還在佔用着;服務沒斷,可以不用重新初始化。