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

 更新时间:2014年02月18日 10:23:35   作者:  
这篇文章主要介绍了java使用smslib连接短信猫发关短信代码,需要的朋友可以参考下

复制代码 代码如下:

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.smslib.ICallNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOutboundMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Library;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

/**
 * @author terry
 *
 */
public class SmsModem {

 // 短信网关
 private SerialModemGateway gateway = null;
 java.util.ResourceBundle rb = null;//ResourceBundle.getBundle("SMS");
 static SmsModem smsModem = null;
 OutboundNotification outboundNotification = new OutboundNotification();
 private static final Logger LOG = Logger.getLogger(SmsModem.class);
 Service srv;
 InboundNotification inboundNotification = new InboundNotification();
 // Create the notification callback method for inbound voice calls.
 CallNotification callNotification = new CallNotification();

 public SmsModem() {
  try {
   //ReadMessages rm = new ReadMessages();
   //rm.doIt();

   rb = ResourceBundle.getBundle("sms");
   String portName= "COM10";
   int port = 9600;
   LOG.info("default portName:" + portName);
   LOG.info("default port:" + port);
   if(rb != null)
   {
    LOG.info("RB is not null");
    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 = new SerialModemGateway("modem." + portName, portName, port,
     "wavecom", "17254");

  } catch (Exception e) {
   LOG.error("网关初始化失败:" + e.getMessage());
   e.printStackTrace();
  }
 }

 public static SmsModem getInstant() {
  if (smsModem == null) {
   smsModem = new SmsModem();
  }
  return smsModem;
 }

 public SerialModemGateway getGateway() {
  return gateway;
 }

 public void sendMessage(String phone, String content) throws Exception {
  doIt(phone, content);
 }

 /**
  * 发送短信
  * @param phone
  * @param content
  * @throws Exception
  */
 public void doIt(String phone, String content) throws Exception {

  OutboundMessage msg;

  LOG.info("Sent Example: Send message from a serial gsm modem.");
  LOG.info(Library.getLibraryDescription());
  LOG.info("Sent Version: " + Library.getLibraryVersion());
  if (srv == null) {
   srv = new Service();
   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("Sent Modem Information:");
   LOG.info("Sent  Manufacturer: " + gateway.getManufacturer());
   LOG.info("Sent  Model: " + gateway.getModel());
   LOG.info("Sent  Serial No: " + gateway.getSerialNo());
   LOG.info("Sent  SIM IMSI: " + gateway.getImsi());
   LOG.info("Sent  Signal Level: " + gateway.getSignalLevel() + "%");
   LOG.info("Sent  Battery Level: " + gateway.getBatteryLevel() + "%");
  }
  // Send a message synchronously.

  msg = new OutboundMessage(phone, content);
  msg.setEncoding(MessageEncodings.ENCUCS2);// 这句话是发中文短信必须的
  srv.sendMessage(msg);
 }

 /**
  * 发送消息类
  * @author terry
  *
  */
 public class OutboundNotification implements IOutboundMessageNotification {
  public void process(String gatewayId, OutboundMessage msg) {
   LOG.info("Sent Outbound handler called from Gateway: " + gatewayId);
   LOG.info(msg);
  }
 }
 //接收消息类
 public String readMessage()
 {
  StringBuffer sb = new StringBuffer("");
  List<InboundMessage> msgList;
  // Create the notification callback method for Inbound & Status Report
  // messages.

  try
  {
   System.out.println("Read Example: Read messages from a serial gsm modem.");
   System.out.println(Library.getLibraryDescription());
   System.out.println("Read Version: " + Library.getLibraryVersion());
   // Create new Service object - the parent of all and the main interface
   // to you.
   if (srv == null) {
    srv = new Service();
    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, you may define as many Gateway objects, representing
   // various GSM modems, add them in the Service object and control all of them.
   //
   // Start! (i.e. connect to all defined Gateways)

   LOG.info("Read Modem Information:");
   LOG.info("Read   Manufacturer: " + gateway.getManufacturer());
   LOG.info("Read   Model: " + gateway.getModel());
   LOG.info("Read   Serial No: " + gateway.getSerialNo());
   LOG.info("Read   SIM IMSI: " + gateway.getImsi());
   LOG.info("Read   Signal Level: " + gateway.getSignalLevel() + "%");
   LOG.info("Read   Battery Level: " + gateway.getBatteryLevel() + "%");
   // Read Messages. The reading is done via the Service object and
   // affects all Gateway objects defined. This can also be more directed to a specific
   // Gateway - look the JavaDocs for information on the Service method calls.
   msgList = new ArrayList<InboundMessage>();
   this.srv.readMessages(msgList, MessageClasses.ALL);
   int num = 1;
   for (InboundMessage msg : 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);
   }
   // Sleep now. Emulate real world situation and give a chance to the notifications
   // methods to be called in the event of message or voice call reception.
   //System.out.println("Now Sleeping - Hit <enter> to terminate.");
   //System.in.read();
  }
  catch (Exception e)
  {
   sb.append(e.getMessage());
   e.printStackTrace();
  }
  finally
  {
   //this.srv.stopService();
  }
  return sb.toString();
 }

 public class InboundNotification implements IInboundMessageNotification
 {
  public void process(String gatewayId, MessageTypes msgType, InboundMessage msg)
  {
   if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gatewayId);
   else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gatewayId);
   System.out.println(msg);
   try
   {
    // Uncomment following line if you wish to delete the message upon arrival.
    // srv.deleteMessage(msg);
   }
   catch (Exception e)
   {
    System.out.println("Oops!!! Something gone bad...");
    e.printStackTrace();
   }
  }
 }

 public class CallNotification implements ICallNotification
 {
  public void process(String gatewayId, String callerId)
  {
   System.out.println(">>> New call detected from Gateway: " + gatewayId + " : " + callerId);
  }
 }

}

相关文章

  • jxl操作excel写入数据不覆盖原有数据示例

    jxl操作excel写入数据不覆盖原有数据示例

    网上很多例子,都是用Jxl读或者写excel,本文实现的功能就是将数据源in.xls的第几行第几列数据写入到out.xls的第几行第几列,不覆盖out.xls其他原有的数据。
    2014-03-03
  • SpringBoot解决跨域问题的五种方案

    SpringBoot解决跨域问题的五种方案

    跨域问题指的是不同站点之间,使用 ajax 无法相互调用的问题,跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据,那怎么解决这个问题呢?接下来我们一起来看,需要的朋友可以参考下
    2024-07-07
  • Java流程控制语句最全汇总(中篇)

    Java流程控制语句最全汇总(中篇)

    这篇文章主要介绍了Java流程控制语句最全汇总(中篇),本文章内容详细,通过案例可以更好的理解数组的相关知识,本模块分为了三部分,本次为中篇,需要的朋友可以参考下
    2023-01-01
  • Spring Boot统一处理全局异常的实战教程

    Spring Boot统一处理全局异常的实战教程

    最近在做项目时需要对异常进行全局统一处理,所以下面这篇文章主要给大家介绍了关于Spring Boot统一处理全局异常的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • SpringCloud Gateway动态转发后端服务实现过程讲解

    SpringCloud Gateway动态转发后端服务实现过程讲解

    这篇文章主要介绍了SpringCloud Gateway动态转发后端服务实现过程,简单的路由转发可以通过SpringCloudGateway的配置文件实现,在一些业务场景种,会需要动态替换路由配置中的后端服务地址,单纯靠配置文件无法满足这种需求
    2023-03-03
  • 举例讲解Java设计模式中的对象池模式编程

    举例讲解Java设计模式中的对象池模式编程

    这篇文章主要介绍了Java设计模式中的对象池模式编程示例分享,对象池模式经常在多线程开发时被用到,需要的朋友可以参考下
    2016-02-02
  • Java实现的图像查看器完整实例

    Java实现的图像查看器完整实例

    这篇文章主要介绍了Java实现的图像查看器,以完整实例形式较为详细的分析了java处理图片的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • SpringMVC实现全局异常处理器的经典案例

    SpringMVC实现全局异常处理器的经典案例

    文章介绍了如何使用@ControllerAdvice和相关注解实现SpringMVC的全局异常处理,通过统一的异常处理类和自定义业务异常类,可以将所有控制器的异常集中处理,并以JSON格式返回给前端,感兴趣的朋友一起看看吧
    2025-03-03
  • IDEA创建springboot + mybatis项目全过程(步骤详解)

    IDEA创建springboot + mybatis项目全过程(步骤详解)

    这篇文章主要介绍了IDEA创建springboot + mybatis项目全过程及步骤详解,本文通图文实例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • HttpClient的RedirectStrategy重定向处理核心机制

    HttpClient的RedirectStrategy重定向处理核心机制

    这篇文章主要为大家介绍了HttpClient的RedirectStrategy重定向处理核心机制源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10

最新评论