步驟1.將短信貓插入到USB接口中,可以先不放sim卡,然後打開設備管理器,裏面會有要安裝的驅動的顯示,為了安裝驅動簡單,可以下載驅動精靈來自動安裝,按裝完成後,
安裝完畢後,打開設備管理器。如圖
com1即為短信貓端口,如要修改端口號,則在屬性,高級裏修改端口號。
步驟2:
下載兩個包:javacomm20-win32.zip和smslib-v3.5.1.zip
配置系統環境變量 %JDK_HOME%,然後拷貝所需的文件至
%JDK_HOME%\jre\bin\win32com.dll
%JDK_HOME%\jre\lib\javax.comm.properties
JAVA_HOME是jdk的路徑,而非jre,
還有特別要注意:用myeclipse8.5等的朋友要注意了你看看你用的ide是否是用自己自帶的jdk的,可以將上面的文件拷貝到該ide自帶的jdk相應的目錄下
3.新建工程
先建一個CommTest 類來測試端口是否能找到,若沒找到,則前面的配置還是有問題的,繼續配置吧
// JindiSMS v3.1 // 該程序主要用於GSM Modem 串口連接速率測試。
1 import gnu.io.*;
2
3 import javax.comm.*;
4
5 import java.util.*;
6
7 import java.io.*;
8
9 public class CommTest {
10
11 static CommPortIdentifier portId;
12
13 static Enumeration portList;
14
15 static int bauds[] = { 9600, 19200, 57600, 115200 };
16
17
18
19 public static void main(String[] args) {
20
21 portList = CommPortIdentifier.getPortIdentifiers();
22
23 System.out.println("金笛短信設備端口連接測試...");
24
25 while (portList.hasMoreElements()) {
26
27 portId = (CommPortIdentifier) portList.nextElement();
28
29 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
30
31 System.out.println("找到串口: " + portId.getName());
32
33 for (int i = 0; i < bauds.length; i++) {
34
35 System.out.print(" Trying at " + bauds[i] + "...");
36
37 try
38
39 {
40
41 SerialPort serialPort;
42
43 InputStream inStream;
44
45 OutputStream outStream;
46
47 int c;
48
49 String response;
50
51 serialPort = (SerialPort) portId.open("SMSLibCommTester", 1971);
52
53 serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
54
55 serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
56
57 inStream = serialPort.getInputStream();
58
59 outStream = serialPort.getOutputStream();
60
61 serialPort.enableReceiveTimeout(1000);
62
63 c = inStream.read();
64
65 while (c != -1)
66
67 c = inStream.read();
68
69 outStream.write('A');
70
71 outStream.write('T');
72
73 outStream.write('\r');
74
75 try
76
77 {
78
79 Thread.sleep(1000);
80
81 }
82
83 catch (Exception e)
84
85 {
86
87 }
88
89 response = "";
90
91 c = inStream.read();
92
93 while (c != -1)
94
95 {
96
97 response += (char) c;
98
99 c = inStream.read();
100
101 }
102
103 f (response.indexOf("OK") >= 0)
104
105 {
106
107 try
108
109 {
110
111 System.out.print(" 獲取設備信息...");
112
113 outStream.write('A');
114
115 outStream.write('T');
116
117 outStream.write('+');
118
119 outStream.write('C');
120
121 outStream.write('G');
122
123 outStream.write('M');
124
125 outStream.write('M');
126
127 outStream.write('\r');
128
129 response = "";
130
131 c = inStream.read();
132
133 while (c != -1) {
134
135 response += (char) c;
136
137 c = inStream.read();
138
139 }
140
141 System.out.println(" 發現設備: " + response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", ""));
142
143 } catch (Exception e)
144
145 {
146
147 System.out.println(" 沒有發現設備!");
148
149 }
150
151 }
152
153 else System.out.println(" 沒有發現設備!");
154
155 serialPort.close();
156
157 } catch (Exception e)
158
159 {
160
161 System.out.println(" 沒有發現設備!");
162
163 } } } } } }
找到端口後就可以來測試發接短信了
1.發短信
// JindiSMS v3.1 // 該程序主要用於GSM Modem短信發送示例。 為節省您的開發時間,請配合金笛短信設備以達到最佳效果。
1 import cn.sendsms.*;
2
3 import java.util.*; import java.io.*; import java.net.*;
4
5 public class SendMessage { public void doIt() throws Exception { Service srv; OutboundMessage msg; // SerialModemGateway gateway=null; OutboundNotification outboundNotification = new OutboundNotification();
6
7 System.out.println("示例: 通過金笛串口短信貓發送短信."); System.out.println(Library.getLibraryDescription()); System.out.println("版本: " + Library.getLibraryVersion());
8
9 srv = new Service(); SerialModemGateway gateway = new SerialModemGateway("jindi", "COM3", 115200, "wavecom", "M1306B", srv.getLogger()); gateway.setInbound(true); gateway.setOutbound(true); gateway.setSimPin("0000"); gateway.setOutboundNotification(outboundNotification); srv.addGateway(gateway);
10
11 srv.startService(); System.out.println(); System.out.println("GSM Modem信息:"); System.out.println(" 廠家: " + gateway.getManufacturer()); System.out.println(" 型號: " + gateway.getModel()); System.out.println(" 序列號: " + gateway.getSerialNo()); System.out.println(" SIM IMSI: " + gateway.getImsi()); System.out.println(" 信號強度: " + gateway.getSignalLevel() + "%"); System.out.println(" 電池容量: " + gateway.getBatteryLevel() + "%"); System.out.println();
12
13 //15988765103 //發送短信(異步發送,待發送短信進入隊列,發送時不做任何等待) msg = new OutboundMessage("15988765103", "Wrong number tl!"); msg.setPriority(msg.getPriority().HIGH); srv.queueMessage(msg, gateway.getGatewayId()); msg = new OutboundMessage("15067851967", "Wrong number xm!"); msg.setPriority(msg.getPriority().LOW); srv.queueMessage(msg, gateway.getGatewayId()); System.out.println("進入等待狀態... - 按回車鍵退出."); System.in.read();
14
15 srv.stopService(); }
16
17 public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("發送狀態: " + gatewayId); System.out.println(msg); } }
18
19 public static void main(String args[]) { SendMessage app = new SendMessage(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }
2.收短信
// JindiSMS v3.1 // 該程序主要用於讀取GSM Modem接收到的短信。
1 import java.util.*; import cn.sendsms.*;
2
3 public class ReadMessages { private Service srv;
4
5 public void doIt() throws Exception { List msgList; InboundNotification inboundNotification = new InboundNotification(); CallNotification callNotification = new CallNotification(); try { System.out.println("示例: 從串口短信設備讀取短信."); System.out.println(Library.getLibraryDescription()); System.out.println("版本: " + Library.getLibraryVersion()); srv = new Service(); // 創建串口GSM modem連接通道. SerialModemGateway gateway = new SerialModemGateway("jindi", "COM5", 115200, "wavecom", "M1206B", srv.getLogger()); //設置通道是否接收短信 gateway.setInbound(true); //設置通道是否可以發送短信 gateway.setOutbound(true); gateway.setSimPin("0000"); //設置短信到達後調用方法 gateway.setInboundNotification(inboundNotification); gateway.setCallNotification(callNotification); // 增加短信通道到服務對象 srv.addGateway(gateway); // 如果有多個設備,可以依次添加到服務對象。
6
7 // 啓動服務! (連接到所有已定義的設備通道) srv.startService(); System.out.println(); System.out.println("GSM Modem 信息:"); System.out.println(" 廠家: " + gateway.getManufacturer()); System.out.println(" 型號: " + gateway.getModel()); System.out.println(" 序列號: " + gateway.getSerialNo()); System.out.println(" SIM IMSI: " + gateway.getImsi()); System.out.println(" 信號強度: " + gateway.getSignalLevel() + "%"); System.out.println(" 電池容量: " + gateway.getBatteryLevel() + "%"); System.out.println(); // 讀取短信. msgList = new ArrayList(); srv.readMessages(msgList, MessageClasses.ALL); System.out.println(msgList.size()); for (int i = 0; i < msgList.size(); i++) System.out.println(msgList.get(i)); // 進入休眠狀態. 如果有新短信進來,就會重新激活。 System.out.println("進入等待狀態... - 按 <enter>回車鍵退出."); System.in.read(); } catch (Exception e) { e.printStackTrace(); } finally { srv.stopService(); } }
8
9 public class InboundNotification implements IInboundMessageNotification { public void process(String gatewayId, MessageTypes msgType, String memLoc, int memIndex) { List msgList; if (msgType == MessageTypes.INBOUND) { System.out.println(">>> 監測到設備收到新的短信: " + gatewayId + " : " + memLoc + " @ " + memIndex); try { // Read... msgList = new ArrayList(); srv.readMessages(msgList, MessageClasses.UNREAD, gatewayId); for (int i = 0; i < msgList.size(); i++) System.out.println(msgList.get(i)); // ...and reply. // for (int i = 0; i < msgList.size(); i ++) // srv.sendMessage(new OutboundMessage(msgList.get(i).getOriginator(), "Got it!"), gatewayId); } catch (Exception e) { System.out.println("有異常..."); e.printStackTrace(); } } else if (msgType == MessageTypes.STATUSREPORT) { System.out.println(">>> 監測到設備收到短信狀態報告: " + gatewayId + " : " + memLoc + " @ " + memIndex); } } }
10
11 public class CallNotification implements ICallNotification { public void process(String gatewayId, String callerId) { System.out.println(">>> 監測到有電話打入: " + gatewayId + " : " + callerId); } }
12
13 public static void main(String args[]) { ReadMessages app = new ReadMessages(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }