短信猫jindi java sms for linux V3.1

来源:互联网 发布:c语言黄金矿工 编辑:程序博客网 时间:2024/05/05 20:28

1.jindi java sms for linux V3.1(JJSL) 安装说明

http://blog.csdn.net/fjfdszj/archive/2010/06/18/5677469.aspx

 

2.相关软件下载

http://download.csdn.net/source/2463995

 

3.因为短信猫,没有像信息机有长号支持,所以反馈时必须通过加验证码实现.用户上行需要变成:验证码+内容

整合代码实例:

//==========================全局定义如下变量=============================
 //短信貓
 Service srv;//服務
 OutboundMessage msg;//消息
 //回調函數
 OutboundNotification outboundNotification = new OutboundNotification();//发送的回调函数
 InboundNotification inboundNotification = new InboundNotification();        //接收短信的回调函数
 CallNotification callNotification = new CallNotification();                               //接听电话的回调函数

//===========================连接初始化============================

 private void ProBaseConf() throws Exception{
  srv = new Service();
  //配置短信猫信息
  SerialModemGateway gateway = new SerialModemGateway("jindi", "/dev/ttyS0", 9600, "wavecom", "M1306B", srv.getLogger());
  gateway.setInbound(true);
  gateway.setOutbound(true);
  gateway.setSimPin("0000");
  //设置发送回调方法
  gateway.setOutboundNotification(outboundNotification);
  //设置短信到达后调用方法
  gateway.setInboundNotification(inboundNotification);
  gateway.setCallNotification(callNotification);
  //将配置加入服务中
  srv.addGateway(gateway);
  //启动服务
  srv.startService();
  System.out.println("启动服务srv.startService()!");
  
 }

//============================发送短信===============================

 public int SendMessage(SmsBean sms)
 {
  int flag=0;

  //手机号列表
  ArrayList telList = sms.getMobileNoList();
  //短信内容
  String tmpContent = sms.getSmsContent();
  //如果是长号,加验证码
  if (sms.getSn().length() == 6) {
   //组装成6位,1位命令字+5位自动增加的.
   tmpContent = tmpContent+",验证码:"+sms.getSn();//验证码下行,同时下行
  }
  
  for (int i = 0; i < telList.size(); i++) {
   try {
    msg = new OutboundMessage(telList.get(i).toString(), tmpContent);
    msg.setEncoding(MessageEncodings.ENCUCS2);
    srv.sendMessage(msg);
    flag=1;
   } catch (TimeoutException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    CloseService();
   } catch (GatewayException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    CloseService();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    CloseService();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    CloseService();
   }
  }
  return flag;
 }
 //============================发送短信状态报告===============================
 public class OutboundNotification implements IOutboundMessageNotification
 {
  public void process(String gatewayId, OutboundMessage msg)
  {
   System.out.println("发送状态: " + gatewayId);
   System.out.println(msg);
  }
 }

 

//============================接收短信===============================

//接收短信报告
 public class InboundNotification implements IInboundMessageNotification
 {
  public void process(String gatewayId, MessageTypes msgType, String memLoc, int memIndex)
  {
   if (msgType == MessageTypes.INBOUND)
   {
    //gatewayId,是硬件设备名称
    System.out.println(">>> 监测到设备收到新的短信: " + gatewayId + " : " + memLoc + " @ " + memIndex);
    try
    {
     List msgList = new ArrayList();//存放接收信息
     srv.readMessages(msgList, MessageClasses.UNREAD, gatewayId);//将接收信息写入
     String[] msg=new String[2];//第1位存内容,第2位存手机

      for (int i = 0; i < msgList.size(); i++)
      {
       String cnt=msgList.get(i).toString();//这个内容包括手机号,客户上行内容,需要对其进行分析提取.
       //去除接收的回车,换行,制表格等信息.
       Pattern p = Pattern.compile("/t|/r|/n");
       Matcher mat = p.matcher(cnt.toString());
       cnt = mat.replaceAll("");
       //处理信息内容
       msg[0]=cnt.substring(cnt.indexOf("短信内容:")+5, cnt.indexOf("发送者:"));//取得短信内容
       msg[1]=cnt.substring(cnt.indexOf("发送者:")+8, cnt.indexOf("短信编号:"));//取得手机号
       RecPro(msg);
      }
    }
    catch (Exception e)
    {
     System.out.println("有异常...");
     e.printStackTrace();
     //输出提示
     System.out.println(msg);
    }
   }
   else if (msgType == MessageTypes.STATUSREPORT)
   {
    System.out.println(">>> 监测到设备收到短信状态报告: " + gatewayId + " : " + memLoc + " @ " + memIndex);
   }
  }
 }

 

//============================关闭短信猫===============================

 private void CloseService(){
  try {
   srv.stopService();
  } catch (TimeoutException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (GatewayException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

//***********************************************************************

//*******接收短信修正(包括接收后短信删除,因为过多存储的短信可能导致异常)****************

//=========================== 2010-06-24============================

//接收短信报告
 public class InboundNotification implements IInboundMessageNotification
 {
  public void process(String gatewayId, MessageTypes msgType, String memLoc, int memIndex)
  {
   if (msgType == MessageTypes.INBOUND)
   {
    //gatewayId,是硬件设备名称
    System.out.println(">>> 监测到设备收到新的短信: " + gatewayId + " : " + memLoc + " @ " + memIndex);
    try
    {
     List msgList = new ArrayList();//存放接收信息
     srv.readMessages(msgList, MessageClasses.UNREAD, gatewayId);//将接收信息写入
     String[] msg=new String[2];//第1位存内容,第2位存手机

      for (int i = 0; i < msgList.size(); i++)
      {
       //删除该短信方法2010-06-24
       InboundMessage inb=(InboundMessage)msgList.get(i);

       //处理信息内容
       msg[0]=inb.getText();//取得短信内容
       msg[1]=inb.getOriginator();//取得手机号 +13512345678
       RecPro(msg);

       srv.deleteMessage(inb);//删除短信内容
      }
    }
    catch (Exception e)
    {
     System.out.println("有异常...");
     e.printStackTrace();
     //输出提示
     System.out.println(msg);
    }
   }
   else if (msgType == MessageTypes.STATUSREPORT)
   {
    System.out.println(">>> 监测到设备收到短信状态报告: " + gatewayId + " : " + memLoc + " @ " + memIndex);
   }
  }
 }

 

原创粉丝点击