一、主要就用到三个包:

1、log4j-1.2.16.jar
2、smslib-3.5.1.jar
3、comm.jar(这个不需要拷到lib下面)

二、在Windows环境下使用SMSLib编程的时候,我们需要做一下comm的配置:

1、将win32com.dll放置在%JAVA_HOME%\jre\bin下
2、将comm.jar放置在%JAVA_HOME%\jre\lib\ext下
3、将javax.comm.properties放置在%JAVA_HOME%\jre\lib下
上面三个文件可以去http://smslib.googlecode.com/files/javacomm20-win32.zip这里下载

然后下面是两个发送与接收短信的示例代码(经过一些修改)

SendMessage.java

package ob;import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;public class SendMessage {public class OutboundNotification implements IOutboundMessageNotification {public void process(AGateway agateway, OutboundMessage outboundmessage) {System.out.println("Outbound handler called from Gateway: " + agateway);System.out.println(outboundmessage);}}@SuppressWarnings("deprecation")public void sendSMS(String mobilePhones, String content) throws GatewayException {Service srv;OutboundMessage msg;OutboundNotification outboundNotification = new OutboundNotification();
//      srv = new Service();srv = Service.getInstance();SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "wavecom", ""); // 设置端口与波特率gateway.setInbound(true);gateway.setOutbound(true);gateway.setSimPin("1234");
//      gateway.setOutboundNotification(outboundNotification);srv.setOutboundMessageNotification(outboundNotification);srv.addGateway(gateway);System.out.println("初始化成功,准备开启服务");try {srv.startService();System.out.println("服务启动成功");String[] phones = mobilePhones.split(",");for (int i = 0; i < phones.length; i++) {msg = new OutboundMessage(phones[i], content);msg.setEncoding(MessageEncodings.ENCUCS2); // 中文srv.sendMessage(msg);}srv.stopService();
srv.removeGateway(gateway);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) throws GatewayException {SendMessage sendMessage = new SendMessage();sendMessage.sendSMS("13808080808", "短信内容");}
}

ReadMessages.java

package ob;import java.util.ArrayList;
import java.util.List;
import javax.crypto.spec.SecretKeySpec;
import org.smslib.AGateway;
import org.smslib.ICallNotification;
import org.smslib.IGatewayStatusNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOrphanedMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.Library;
import org.smslib.Service;
import org.smslib.AGateway.GatewayStatuses;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Message.MessageTypes;
import org.smslib.crypto.AESKey;
import org.smslib.modem.SerialModemGateway;public class ReadMessages {public static Service srv = Service.getInstance();public void doIt() throws Exception {List<InboundMessage> msgList;InboundNotification inboundNotification = new InboundNotification();CallNotification callNotification = new CallNotification();GatewayStatusNotification statusNotification = new GatewayStatusNotification();OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();try {System.out.println("Example: Read messages from a serial gsm modem.");System.out.println(Library.getLibraryDescription());System.out.println("Version: " + Library.getLibraryVersion());SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, null, null);gateway.setProtocol(Protocols.PDU);gateway.setInbound(true);gateway.setOutbound(true);srv.setInboundMessageNotification(inboundNotification);srv.setCallNotification(callNotification);srv.setGatewayStatusNotification(statusNotification);srv.setOrphanedMessageNotification(orphanedMessageNotification);srv.addGateway(gateway);srv.startService();System.out.println();System.out.println("Modem Information:");System.out.println(" Manufacturer: " + gateway.getManufacturer());System.out.println(" Model: " + gateway.getModel());System.out.println(" Serial No: " + gateway.getSerialNo());System.out.println(" SIM IMSI: " + gateway.getImsi());System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%");System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");System.out.println();srv.getKeyManager().registerKey("+8613808080808", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));msgList = new ArrayList<InboundMessage>();srv.readMessages(msgList, MessageClasses.ALL);for (InboundMessage msg : msgList) {System.out.println(msg);
//              srv.deleteMessage(msg);     //删除短信}System.out.println("Now Sleeping - Hit <enter> to stop service.");System.in.read();System.in.read();} catch (Exception e) {e.printStackTrace();}}public class InboundNotification implements IInboundMessageNotification {public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) {if (msgType == MessageTypes.INBOUND)System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());else if (msgType == MessageTypes.STATUSREPORT)System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());System.out.println(msg);}}public class CallNotification implements ICallNotification {public void process(AGateway gateway, String callerId) {System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);}}public class GatewayStatusNotification implements IGatewayStatusNotification {public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus) {System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);}}public class OrphanedMessageNotification implements IOrphanedMessageNotification {public boolean process(AGateway gateway, InboundMessage msg) {System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());System.out.println(msg);return false;}}public static void main(String args[]) {ReadMessages app = new ReadMessages();try {app.doIt();} catch (Exception e) {e.printStackTrace();}}
}

上面代码在第一次运行都是正常的,有个问题是,用的是usb接口的短信猫,好像是停止不了服务的,第二次接收或者发送,总会报一个错:

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by org.smslib
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
    at org.smslib.Service$1Starter.run(Service.java:276)

找了很多资料,都没找到解决方法。估计只有串口的短信猫设备才能正常。。

目前是把Service一直开启,不做停止,然后一直重复读取短信,弄了个循环,临时解决。

会报错是因为没有移除端口,在srv.stopService();后面加一句srv.removeGateway(gateway);即可。

短信猫接收与发送短信整理相关推荐

  1. Android接收和发送短信

    每一部手机都具有短信接收和发送功能,下面我们通过代码来实现接收和发送短信功能. 一.接收短信 1.创建内部广播接收器类,接收系统发出的短信广播 2.从获得的内容中解析出短信发送者和短信内容 3.在Ac ...

  2. android 华为 发送短信,增强信息来了!华为短信App全免费发送短彩信

    IT之家7月3日消息 刚刚,华为官方宣布,推出增强信息.现在使用华为短信App就可以免费发送短彩信了. 华为在EMUI 8.0推出融合通信,短信App已开启"增强信息"模式,增强信 ...

  3. thinkphp6对接阿里云短信服务完成定时发送短信功能

    1.导入阿里短信包 通过composer下载依赖包 composer require alibabacloud/dysmsapi-20170525 2.0.22 具体可参见阿里云短信服务 >&g ...

  4. android手机不能发短信,小米4手机收短信正常但无法发送短信怎么办?

    小编的手机最近突然出现了这样一个问题,什么也没动,突然就无法发送短信了,而状况是一切都正常,收短信也正常,只是发不出去.后来去网上查询,原来很多网友都和小编遇到同样的问题,经过一番折腾,终于找到了解决 ...

  5. 8口短信猫设备支持的短信猫软件丰富可满足多种应用需求

    8口短信猫设备支持的短信猫软件丰富可满足多种应用需求,8口短信猫是指可以插入8张SIM手机卡同时使用的,性能.效率是单口的8倍,支持大小手机卡,通过一根USB数据线连接电脑,支持台式笔记本电脑,使用简 ...

  6. springboot集成阿里云短信服务,实现发送短信功能

    springboot集成阿里云短信服务,实现发送短信功能 准备工作: 1.登陆阿里云->进入控制台->开通短信服务(进入后根据提示开通) 2.充值(借人家平台发短信你以为不要钱的?我充了3 ...

  7. wavecom短信猫推荐RS232串口短信猫适于二次开发应用

    wavecom短信猫是指采用wavecom模块生产的短信猫设备,RS232是基于标准串口连接,与电脑串口直连,免驱动性能稳定,适于短信猫二次开发应用.兼容性好,支持的短信猫软件产品丰富. ‍‍基于RS ...

  8. python发送短信验证码登录_python发送短信验证码

    原标题:python发送短信验证码 业务: 手机端点击发送验证码,请求发送到python端,由python调用榛子云短信http://smsow.zhenzikj.com的短信接口,生成验证码并发送. ...

  9. 短信验证码原理java_[java发送短信验证码原理]java发送短信验证码

    业务: 手机端点击发送验证码,请求发送到java服务器端,由java调用第三方平台(我们使用的是榛子云短信http://smsow.zhenzikj.com)的短信接口,生成验证码并发送. 下载后的S ...

最新文章

  1. matlab图像定位分割,車牌定位matlab程序:通過hsv彩色分割方式定位車牌
  2. delphi 回调函数
  3. 使用Hibernate编写通用数据库操作代码
  4. pppoe错误代码 linux,PPPOE常见故障代码及分析
  5. Bootstrap组件_巨幕,页头,缩略图
  6. Content Security Policy 入门教程
  7. java.util.UnknownFormatConversionException: Conversion = ‘j‘ || Conversion = ‘D‘ || Conversion = ‘Y‘
  8. linux下toe网卡驱动,toe命令是干什么的,有没有大神解答一下
  9. 和doc的区别怎么转换手机_如何把doc文件转换成PDF格式?高手告诉你应该这么做...
  10. asp.net获取服务器信息,Asp.netnbsp;获取服务器信息, 站长资讯平台
  11. 1929. 数组串联
  12. Windows RPC Demo实现
  13. 如何将pdf修改编辑
  14. RN中热更新CodePush使用
  15. 从产品角度,快速接盘新系统的一些经验及方法提炼
  16. 《神经科学:探索脑》学习笔记(第23章 记忆系统)
  17. Android代码实现打开打开wifi wps按钮和wps pin码输入
  18. uboot启动第一阶段——start.S(二)
  19. 单片机8位抢答器实训机电报告_16路抢答器单片机实训报告.docx
  20. 行到水穷处,坐看云起时

热门文章

  1. java算法int型整数反转的另类解法
  2. 【es】es界面化管理工具cerebro的安装和使用
  3. angular的 #
  4. 数据结构---散列表(哈希表)链地址法
  5. GDOI2021自闭记
  6. OpenLayer仿天地图多时相
  7. java项目任务跟踪系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  8. 战双帕弥什qq登录服务器未响应是什么意思,战双帕弥什qq登录
  9. 百度地图3d效果和卫星图效果
  10. 小米路由器 你的连接不是专用连接