SMSLib开发指南.docx
- 文档编号:9941053
- 上传时间:2023-05-22
- 格式:DOCX
- 页数:24
- 大小:406.05KB
SMSLib开发指南.docx
《SMSLib开发指南.docx》由会员分享,可在线阅读,更多相关《SMSLib开发指南.docx(24页珍藏版)》请在冰点文库上搜索。
SMSLib开发指南
SMSLib开发指南
一、SMSLib简介
SMSLib是一个开放源代码的短信猫二次开发包,有JAVA和.Net两个版本,目前最新版为v3.5.2。
图1-1
二、Window平台
1、在smslib官网下载“SMSLibforJavav3.5.2”开发包,并解压,目录结构如下图所示:
图2-1
图2-2
lib:
存放二次开发包(smslib-3.5.2.jar)和运行时的依赖包(slf4j、log4j、commons-net、jsmpp等)(重要)
dist:
存放短信猫服务开发包(smsserver-3.5.2.jar),该包包括了smslib-3.5.2.jar中的所有核心类。
如果是将短信猫作为服务的方式部署,不需要额外写代码开发短信发送和接收的接口,直接部署短信服务即可,详细的步聚,可以参考《短信猫服务安装与配置指南》。
(重要)
doc:
smslib介绍、使用指南、smsserver安装与配置等文档(重要)
javadoc:
二次开发包API
src:
存放二次开发包源码和示例源码
misc:
smslib日志(log4j)配置配置模板、smsserver数据库建库脚本及服务接口等文件
build:
项目管理相关文件(不重要)
2、下载SUNJavaCommv2(Win32)动态库,并解压,目录结构如下图所示:
图3-1
3、运行环境配置
∙复制“图3-1”中m.properties文件到%JAVA_HOME%\jre\lib目录下,win32com.dll文件到%JAVA_HOME%\jre\bin目录下
∙复制“图3-1”中comm.jar和图2-2中所有jar文件到CLASSPATH目录下(如果是用eclipse等IDE工具,将这些jar包导入到工程中)
注意:
win32com.dll只支持32位jdk
4、运行示例程序并测试
修改图2-1中src\java\examples\modem目录下的SendMessage.java和ReadMessages.java程序发送短信的参数配置,编译并运行。
如下图所示:
[java]viewplaincopy
1//SendMessage.java-Sampleapplication.
2//短信发送测试程序
3//Thisapplicationshowsyouthebasicprocedureforsendingmessages.
4//Youwillfindhowtosendsynchronousandasynchronousmessages.
5//
6//Forasynchronousdispatch,theexampleapplicationsetsacallback
7//notification,toseewhat'shappenedwithmessages.
8
9packageexamples.modem;
10
11importorg.smslib.AGateway;
12importorg.smslib.IOutboundMessageNotification;
13importorg.smslib.Library;
14importorg.smslib.OutboundMessage;
15importorg.smslib.Service;
16importorg.smslib.modem.SerialModemGateway;
17
18publicclassSendMessage
19{
20publicvoiddoIt()throwsException
21{
22OutboundNotificationoutboundNotification=newOutboundNotification();
23System.out.println("Example:
Sendmessagefromaserialgsmmodem.");
24System.out.println(Library.getLibraryDescription());
25System.out.println("Version:
"+Library.getLibraryVersion());
26/*
271:
网关ID(即短信猫端口编号)
28COM4:
串口名称(在window中以COMXX表示端口名称,在linux,unix平台下以ttyS0-N或ttyUSB0-N表示端口名称),通过端口检测程序得到可用的端口
29115200:
串口每秒发送数据的bit位数,必须设置正确才可以正常发送短信,可通过程序进行检测。
常用的有115200、9600
30Huawei:
短信猫生产厂商,不同的短信猫生产厂商smslib所封装的AT指令接口会不一致,必须设置正确.常见的有Huawei、wavecom等厂商
31最后一个参数表示设备的型号,可选
32*/
33SerialModemGatewaygateway=newSerialModemGateway("1","COM4",115200,"Huawei","");
34gateway.setInbound(true);//设置true,表示该网关可以接收短信,根据需求修改
35gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改
36gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234
37//ExplicitSMSCaddresssetisrequiredforsomemodems.
38//BelowisforVODAFONEGREECE-besuretosetyourown!
39gateway.setSmscNumber("+306942190000");//短信服务中心号码
40Service.getInstance().setOutboundMessageNotification(outboundNotification);//发送短信成功后的回调函方法
41Service.getInstance().addGateway(gateway);//将网关添加到短信猫服务中
42Service.getInstance().startService();//启动服务,进入短信发送就绪状态
43System.out.println();
44//打印设备信息
45System.out.println("ModemInformation:
");
46System.out.println("Manufacturer:
"+gateway.getManufacturer());
47System.out.println("Model:
"+gateway.getModel());
48System.out.println("SerialNo:
"+gateway.getSerialNo());
49System.out.println("SIMIMSI:
"+gateway.getImsi());
50System.out.println("SignalLevel:
"+gateway.getSignalLevel()+"dBm");
51System.out.println("BatteryLevel:
"+gateway.getBatteryLevel()+"%");
52System.out.println();
53//Sendamessagesynchronously.
54OutboundMessagemsg=newOutboundMessage("306974000000","HellofromSMSLib!
");//参数1:
手机号码参数2:
短信内容
55Service.getInstance().sendMessage(msg);//执行发送短信
56System.out.println(msg);
57//Or,sendoutaWAPSImessage.
58//OutboundWapSIMessagewapMsg=newOutboundWapSIMessage("306974000000",
59//newURL("http:
//www.smslib.org/"),"VisitSMSLibnow!
");
60//Service.getInstance().sendMessage(wapMsg);
61//System.out.println(wapMsg);
62//Youcanalsoqueuesomeasynchronousmessagestoseehowthecallbacks
63//arecalled...
64//msg=newOutboundMessage("309999999999","Wrongnumber!
");
65//srv.queueMessage(msg,gateway.getGatewayId());
66//msg=newOutboundMessage("308888888888","Wrongnumber!
");
67//srv.queueMessage(msg,gateway.getGatewayId());
68System.out.println("NowSleeping-Hit
69System.in.read();
70Service.getInstance().stopService();
71}
72
73/*
74短信发送成功后,调用该接口。
并将发送短信的网关和短信内容对象传给process接口
75*/
76publicclassOutboundNotificationimplementsIOutboundMessageNotification
77{
78publicvoidprocess(AGatewaygateway,OutboundMessagemsg)
79{
80System.out.println("OutboundhandlercalledfromGateway:
"+gateway.getGatewayId());
81System.out.println(msg);
82}
83}
84
85publicstaticvoidmain(Stringargs[])
86{
87SendMessageapp=newSendMessage();
88try
89{
90app.doIt();
91}
92catch(Exceptione)
93{
94e.printStackTrace();
95}
96}
97}
[java]viewplaincopy
98//ReadMessages.java-Sampleapplication.
99//短信读取程序
100//Thisapplicationshowsyouthebasicprocedureneededforreading
101//SMSmessagesfromyourGSMmodem,insynchronousmode.
102//
103//Operationdescription:
104//Theapplicationsetupthenecessaryobjectsandconnectstothephone.
105//Asafirststep,itreadsallmessagesfoundinthephone.
106//Then,itgoestosleep,allowingtheasynchronouscallbackhandlersto
107//becalled.Furthermore,forcallbackdemonstrationpurposes,itresponds
108//toeachreceivedmessagewitha"GotIt!
"reply.
109//
110//Tasks:
111//1)SetupServiceobject.
112//2)SetuponeormoreGatewayobjects.
113//3)AttachGatewayobjectstoServiceobject.
114//4)Setupcallbacknotifications.
115//5)Run
116
117packageexamples.modem;
118
119importjava.util.ArrayList;
120importjava.util.List;
121importjavax.crypto.spec.SecretKeySpec;
122importorg.smslib.AGateway;
123importorg.smslib.AGateway.GatewayStatuses;
124importorg.smslib.AGateway.Protocols;
125importorg.smslib.ICallNotification;
126importorg.smslib.IGatewayStatusNotification;
127importorg.smslib.IInboundMessageNotification;
128importorg.smslib.IOrphanedMessageNotification;
129importorg.smslib.InboundMessage;
130importorg.smslib.InboundMessage.MessageClasses;
131importorg.smslib.Library;
132importorg.smslib.Message.MessageTypes;
133importorg.smslib.Service;
134importorg.smslib.crypto.AESKey;
135importorg.smslib.modem.SerialModemGateway;
136
137publicclassReadMessages
138{
139publicvoiddoIt()throwsException
140{
141//Definealistwhichwillholdthereadmessages.
142List
143//Createthenotificationcallbackmethodforinbound&statusreport
144//messages.
145InboundNotificationinboundNotification=newInboundNotification();
146//Createthenotificationcallbackmethodforinboundvoicecalls.
147CallNotificationcallNotification=newCallNotification();
148//Createthenotificationcallbackmethodforgatewaystatuses.
149GatewayStatusNotificationstatusNotification=newGatewayStatusNotification();
150OrphanedMessageNotificationorphanedMessageNotification=newOrphanedMessageNotification();
151try
152{
153System.out.println("Example:
Readmessagesfromaserialgsmmodem.");
154System.out.println(Library.getLibraryDescription());
155System.out.println("Version:
"+Library.getLibraryVersion());
156//CreatetheGatewayrepresentingtheserialGSMmodem.
157SerialModemGatewaygateway=newSerialModemGateway("4","COM4",115200,"Huawei","E160");
158//SetthemodemprotocoltoPDU(alternativeisTEXT).PDUisthedefault,anyway...
159gateway.setProtocol(Protocols.PDU);
160//DowewanttheGatewaytobeusedforInboundmessages?
161gateway.setInbound(true);
162//DowewanttheGatewaytobeusedforOutboundmessages?
163gateway.setOutbound(true);
164//LetSMSLibknowwhichistheSIMPIN.
165gateway.setSimPin("0000");
166//Setupthenotificationmethods.
167Service.getInstance().setInboundMessageNotification(inboundNotification);
168Service.getInstance().setCallNotification(callNotification);
169Service.getInstance().setGatewayStatusNotification(statusNotification);
170Service.getInstance().setOrphanedMessageNotification(orphanedMessageNotification);
171//AddtheGatewaytotheServiceobject.
172Service.getInstance().addGateway(gateway);
173//Similarly,youmaydefineasmanyGatewayobjects,representing
174//variousGSMmodems,addthemintheServiceobjectandcontrolallofthem.
175//Start!
(i.e.connecttoalldefinedGateways)
176Service.getInstance().startService();
177//Printoutsomegeneralinformationaboutthemodem.
178System.out.println();
179System.out.println("ModemInformation:
");
180System.out.println("Manufacturer:
"+gateway.getManufacturer());
181System.out.println("Model:
"+gateway.getModel());
182System.out.println("SerialNo:
"+gateway.getSerialNo());
183System.out.println("SIMIMSI:
"+gateway.getImsi());
184System.out.println("SignalLevel:
"+gateway.getSignalLevel()+"dBm");
185System.out.println("BatteryLevel:
"+gateway.getBatteryLevel()+"%");
186System.out.println();
187//Incaseyouworkwithencryptedmessages,itsagoodtimetodeclareyourkeys.
188//CreateanewAESKeywithaknownkeyvalue.
189//RegisteritinKeyManagerinordertokeepitactive.SMSLibwillthenautomatically
190//encrypt/decryptallmessagessendto/receivedfromthisnumber.
191Service.getIn
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SMSLib 开发 指南