步骤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 串口连接速率测试。
import gnu.io.*;
import javax.comm.*;
import java.util.*;
import java.io.*;
public class CommTest {
static CommPortIdentifier portId;
static Enumeration portList;
static int bauds[] = { 9600, 19200, 57600, 115200 };
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("金笛短信设备端口连接测试...");
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("找到串口: " + portId.getName());
for (int i = 0; i < bauds.length; i++) {
System.out.print(" Trying at " + bauds[i] + "...");
try
{
SerialPort serialPort;
InputStream inStream;
OutputStream outStream;
int c;
String response;
serialPort = (SerialPort) portId.open("SMSLibCommTester", 1971);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
inStream = serialPort.getInputStream();
outStream = serialPort.getOutputStream();
serialPort.enableReceiveTimeout(1000);
c = inStream.read();
while (c != -1)
c = inStream.read();
outStream.write('A');
outStream.write('T');
outStream.write('\r');
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
}
response = "";
c = inStream.read();
while (c != -1)
{
response += (char) c;
c = inStream.read();
}
f (response.indexOf("OK") >= 0)
{
try
{
System.out.print(" 获取设备信息...");
outStream.write('A');
outStream.write('T');
outStream.write('+');
outStream.write('C');
outStream.write('G');
outStream.write('M');
outStream.write('M');
outStream.write('\r');
response = "";
c = inStream.read();
while (c != -1) {
response += (char) c;
c = inStream.read();
}
System.out.println(" 发现设备: " + response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", ""));
} catch (Exception e)
{
System.out.println(" 没有发现设备!");
}
}
else System.out.println(" 没有发现设备!");
serialPort.close();
} catch (Exception e)
{
System.out.println(" 没有发现设备!");
} } } } } }
找到端口后就可以来测试发接短信了
1.发短信
// JindiSMS v3.1 // 该程序主要用于GSM Modem短信发送示例。 为节省您的开发时间,请配合金笛短信设备以达到最佳效果。
import cn.sendsms.*;
import java.util.*; import java.io.*; import java.net.*;
public class SendMessage { public void doIt() throws Exception { Service srv; OutboundMessage msg; // SerialModemGateway gateway=null; OutboundNotification outboundNotification = new OutboundNotification();
System.out.println("示例: 通过金笛串口短信猫发送短信."); System.out.println(Library.getLibraryDescription()); System.out.println("版本: " + Library.getLibraryVersion());
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);
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();
//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();
srv.stopService(); }
public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("发送状态: " + gatewayId); System.out.println(msg); } }
public static void main(String args[]) { SendMessage app = new SendMessage(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }
2.收短信
// JindiSMS v3.1 // 该程序主要用于读取GSM Modem接收到的短信。
import java.util.*; import cn.sendsms.*;
public class ReadMessages { private Service srv;
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); // 如果有多个设备,可以依次添加到服务对象。
// 启动服务! (连接到所有已定义的设备通道) 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("进入等待状态... - 按 回车键退出."); System.in.read(); } catch (Exception e) { e.printStackTrace(); } finally { srv.stopService(); } }
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); } } }
public class CallNotification implements ICallNotification { public void process(String gatewayId, String callerId) { System.out.println(">>> 监测到有电话打入: " + gatewayId + " : " + callerId); } }
public static void main(String args[]) { ReadMessages app = new ReadMessages(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }