zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java使用smslib连接短信猫发送短信代码分享

JAVA连接代码 使用 分享 发送 短信
2023-06-13 09:15:18 时间

复制代码代码如下:


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);
  }
 }

}