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

复制代码 代码如下:

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

}

时间: 2024-08-13 14:47:55

java使用smslib连接短信猫发送短信代码分享_java的相关文章

关于短信猫发送短信的问题

问题描述 最近我在网上找了一个调用短信猫发送短信的程序,但在使用过程中,有时发送成功,有时发送不成功.错误如下:org.smslib.GatewayException:GSM:InvalidCREGresponse.atorg.smslib.modem.AModemDriver.waitForNetworkRegistration(AModemDriver.java:396)atorg.smslib.modem.AModemDriver.connect(AModemDriver.java:149

java分形绘制科赫雪花曲线(科赫曲线)代码分享_java

首先我们举个例子:我们可以看到西兰花一小簇是整个花簇的一个分支,而在不同尺度下它们具有自相似的外形.换句话说,较小的分支通过放大适当的比例后可以得到一个与整体几乎完全一致的花簇.因此我们可以说西兰花簇是一个分形的实例.分形一般有以下特质:在任意小的尺度上都能有精细的结构: 太不规则,以至难以用传统欧氏几何的语言描述: (至少是大略或任意地)自相似豪斯多夫维数会大於拓扑维数: 有著简单的递归定义.(i)分形集都具有任意小尺度下的比例细节,或者说它具有精细的结构.(ii)分形集不能用传统的几何语言来

java类中生成jfreechart,返回图表的url地址 代码分享_java

web.xml中设置: 复制代码 代码如下: <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet > <servlet-mapping> <servlet-name>DisplayChart</ser

unicode-短信猫发短信,多字节和Unicode差异?

问题描述 短信猫发短信,多字节和Unicode差异? 我使用短信猫发发送时,加载sms.dll成功,但是调用里面的连接函数时,在多字节集下,可以连接短信猫,到时切换到Unicode情况下时,就不能连接成功! 解决方案 单从你的描述看,看不出什么,unicode是多字节编码的一种,但不是唯一的一种. 你说的多字节到底是什么. 解决方案二: 短信的编码用的是GBK

java-短信猫读取短信和运行时,报的错误

问题描述 短信猫读取短信和运行时,报的错误 Exception in thread "SMSLib-AsyncMessageProcessor : SMSLib" java.lang.StringIndexOutOfBoundsException: String index out of range: -12 at java.lang.String.substring(String.java:1911) at org.smslib.modem.ModemGateway.readMess

短信猫收发短信队列

问题描述 因为短信猫不能同时进行短信的接收和发送,需要用到串行队列,把接收短信和发送短信串行化,有哪位朋友做过没?是不是要建立一个线程管理队列,然后从其它线程往里面抛消息? 解决方案 解决方案二:自己顶一下解决方案三:该回复于2010-02-23 09:43:43被版主删除解决方案四:不是有针对短信猫的api吗?该api里面是有消息队列的哦.解决方案五:引用3楼zhuzhaohui640的回复: 不是有针对短信猫的api吗?该api里面是有消息队列的哦. 买短信猫的时候,他们提供两种开发包:一个

VBA实现 用短信猫发短信时,换行问题

问题描述 向手机中发送AB但手机收到后,显示成了一行AB请问CHR(10)或CHR(13)是不是要改成其他什么代码?下记为程序关键代码:phone="8613630000235"'手机号码message="A"&chr(10)&chr(13)"B"'第一行A换行并回车第二行BRet=SzhtoSms1.YhSendSms("8613800757500",phone,message,0)'发送信息请各位大侠救命!

perl-PERL短信脚本发送短信?

问题描述 PERL短信脚本发送短信? 我想知道,用perl脚本调用短信接口发送短信,之后如何让把接口返回码和数据一起写入到oracle数据库中,存放在一张表中,脚本该怎么写啊? 解决方案 http://blog.sina.com.cn/s/blog_537cd5cd010140i6.html 解决方案二: perl 发送短信 解决方案三: http://luogen33.iteye.com/blog/1181063

java中用中国网建提供的SMS短信平台发送短信

接下来的项目需求中提到需要短信发送功能,以前没有做过,因此便在网上搜了一下.大体上说的都是有三种方法,分别是sina提供的webservice接口.短信mao和中国网建提供的SMS短信平台. 这三种方式,因为第一种和第三种都经过了别的平台转发,因此具有一定的安全隐患,第二种短信mao的方式相对可能要安全一些. 但是短信mao需要购买硬件设备,因此我便只好参考网上的文章先实现了用sms平台发送短信的功能. 这个功能说起来实际上很简单,java代码也很少,只是我在实现的过程中参照网上文章的代码和注释