java使用smslib连接短信猫发送短信代码分享
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.log4j.Logger;
importorg.smslib.ICallNotification;
importorg.smslib.IInboundMessageNotification;
importorg.smslib.IOutboundMessageNotification;
importorg.smslib.InboundMessage;
importorg.smslib.InboundMessage.MessageClasses;
importorg.smslib.Library;
importorg.smslib.Message.MessageEncodings;
importorg.smslib.Message.MessageTypes;
importorg.smslib.OutboundMessage;
importorg.smslib.Service;
importorg.smslib.modem.SerialModemGateway;
/**
*@authorterry
*
*/
publicclassSmsModem{
//短信网关
privateSerialModemGatewaygateway=null;
java.util.ResourceBundlerb=null;//ResourceBundle.getBundle("SMS");
staticSmsModemsmsModem=null;
OutboundNotificationoutboundNotification=newOutboundNotification();
privatestaticfinalLoggerLOG=Logger.getLogger(SmsModem.class);
Servicesrv;
InboundNotificationinboundNotification=newInboundNotification();
//Createthenotificationcallbackmethodforinboundvoicecalls.
CallNotificationcallNotification=newCallNotification();
publicSmsModem(){
try{
//ReadMessagesrm=newReadMessages();
//rm.doIt();
rb=ResourceBundle.getBundle("sms");
StringportName="COM10";
intport=9600;
LOG.info("defaultportName:"+portName);
LOG.info("defaultport:"+port);
if(rb!=null)
{
LOG.info("RBisnotnull");
if(rb.getString("smsport")!=null&&!"".equals(rb.getString("smsport")))
{
portName=rb.getString("smsport");
LOG.info("portName:"+portName);
}
if(rb.getString("smsbolv")!=null&&!"".equals(rb.getString("smsbolv")))
{
port=Integer.valueOf(rb.getString("smsbolv"));
LOG.info("port:"+port);
}
}
//初始化短信网关
gateway=newSerialModemGateway("modem."+portName,portName,port,
"wavecom","17254");
}catch(Exceptione){
LOG.error("网关初始化失败:"+e.getMessage());
e.printStackTrace();
}
}
publicstaticSmsModemgetInstant(){
if(smsModem==null){
smsModem=newSmsModem();
}
returnsmsModem;
}
publicSerialModemGatewaygetGateway(){
returngateway;
}
publicvoidsendMessage(Stringphone,Stringcontent)throwsException{
doIt(phone,content);
}
/**
*发送短信
*@paramphone
*@paramcontent
*@throwsException
*/
publicvoiddoIt(Stringphone,Stringcontent)throwsException{
OutboundMessagemsg;
LOG.info("SentExample:Sendmessagefromaserialgsmmodem.");
LOG.info(Library.getLibraryDescription());
LOG.info("SentVersion:"+Library.getLibraryVersion());
if(srv==null){
srv=newService();
srv.S.SERIAL_POLLING=true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(gateway);
srv.startService();
}
if(gateway!=null){
LOG.info("SentModemInformation:");
LOG.info("Sent Manufacturer:"+gateway.getManufacturer());
LOG.info("Sent Model:"+gateway.getModel());
LOG.info("Sent SerialNo:"+gateway.getSerialNo());
LOG.info("Sent SIMIMSI:"+gateway.getImsi());
LOG.info("Sent SignalLevel:"+gateway.getSignalLevel()+"%");
LOG.info("Sent BatteryLevel:"+gateway.getBatteryLevel()+"%");
}
//Sendamessagesynchronously.
msg=newOutboundMessage(phone,content);
msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
srv.sendMessage(msg);
}
/**
*发送消息类
*@authorterry
*
*/
publicclassOutboundNotificationimplementsIOutboundMessageNotification{
publicvoidprocess(StringgatewayId,OutboundMessagemsg){
LOG.info("SentOutboundhandlercalledfromGateway:"+gatewayId);
LOG.info(msg);
}
}
//接收消息类
publicStringreadMessage()
{
StringBuffersb=newStringBuffer("");
List<InboundMessage>msgList;
//CreatethenotificationcallbackmethodforInbound&StatusReport
//messages.
try
{
System.out.println("ReadExample:Readmessagesfromaserialgsmmodem.");
System.out.println(Library.getLibraryDescription());
System.out.println("ReadVersion:"+Library.getLibraryVersion());
//CreatenewServiceobject-theparentofallandthemaininterface
//toyou.
if(srv==null){
srv=newService();
srv.S.SERIAL_POLLING=true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(gateway);
srv.startService();
}
//Similarly,youmaydefineasmanyGatewayobjects,representing
//variousGSMmodems,addthemintheServiceobjectandcontrolallofthem.
//
//Start!(i.e.connecttoalldefinedGateways)
LOG.info("ReadModemInformation:");
LOG.info("Read Manufacturer:"+gateway.getManufacturer());
LOG.info("Read Model:"+gateway.getModel());
LOG.info("Read SerialNo:"+gateway.getSerialNo());
LOG.info("Read SIMIMSI:"+gateway.getImsi());
LOG.info("Read SignalLevel:"+gateway.getSignalLevel()+"%");
LOG.info("Read BatteryLevel:"+gateway.getBatteryLevel()+"%");
//ReadMessages.ThereadingisdoneviatheServiceobjectand
//affectsallGatewayobjectsdefined.Thiscanalsobemoredirectedtoaspecific
//Gateway-looktheJavaDocsforinformationontheServicemethodcalls.
msgList=newArrayList<InboundMessage>();
this.srv.readMessages(msgList,MessageClasses.ALL);
intnum=1;
for(InboundMessagemsg:msgList)
{
sb.append("第"+num+"条;发件人:"+msg.getOriginator()+";内容:"+msg.getText()+"\n");
//sb.append(msg.toString()+"\n");
LOG.info("第"+num+"条;发件人:"+msg.getOriginator()+";内容:"+msg.getText()+"\n");
num++;
LOG.info(msg);
}
//Sleepnow.Emulaterealworldsituationandgiveachancetothenotifications
//methodstobecalledintheeventofmessageorvoicecallreception.
//System.out.println("NowSleeping-Hit<enter>toterminate.");
//System.in.read();
}
catch(Exceptione)
{
sb.append(e.getMessage());
e.printStackTrace();
}
finally
{
//this.srv.stopService();
}
returnsb.toString();
}
publicclassInboundNotificationimplementsIInboundMessageNotification
{
publicvoidprocess(StringgatewayId,MessageTypesmsgType,InboundMessagemsg)
{
if(msgType==MessageTypes.INBOUND)System.out.println(">>>NewInboundmessagedetectedfromGateway:"+gatewayId);
elseif(msgType==MessageTypes.STATUSREPORT)System.out.println(">>>NewInboundStatusReportmessagedetectedfromGateway:"+gatewayId);
System.out.println(msg);
try
{
//Uncommentfollowinglineifyouwishtodeletethemessageuponarrival.
//srv.deleteMessage(msg);
}
catch(Exceptione)
{
System.out.println("Oops!!!Somethinggonebad...");
e.printStackTrace();
}
}
}
publicclassCallNotificationimplementsICallNotification
{
publicvoidprocess(StringgatewayId,StringcallerId)
{
System.out.println(">>>NewcalldetectedfromGateway:"+gatewayId+":"+callerId);
}
}
}
相关文章
- java queue toarray_Java PriorityBlockingQueue toArray()用法及代码示例
- java卸载 安装错误_Java卸载后无法重新安装 提示已安装过[通俗易懂]
- Contest1620 – 2020-2021-2学期《Java Web 系统开发》:java基础:字符串
- 编写java判断闰年_Java 判断闰年代码实例
- java压缩与解压(Java.util.zip)详解编程语言
- java连接mysql示例代码详解编程语言
- Java连接MySQL数据库的简单步骤(java如何连接mysql数据库)
- Java DriverManager.getConnection()方法:获取数据库连接
- Java联合Redis:建立良好数据连接(java连接redis)
- Oracle与Java的连接:揭秘数据库开发技术之间的神奇联系(oracle连接java)
- 如何在Linux系统下有效地启动Java程序,让你的代码在Linux中也能正常运行?(linux下启动java)
- MSSQL与Java的数据库连接实践(mssql连接java)
- Redis无法与Java建立连接(redis连不上java)
- Java使用JDBC连接Oracle_MSSQL实例代码