Java smslib包开发

上一篇文章我详细介绍RXTXcomm的安装方法和简单代码,如果小伙伴涉及到需要使用手机短信模块完成短信收发需求的话,可以使用到smslib进行开发。

首先还是同样的,将整个smslib包源码导入项目,并且将它所需依赖一起进行导入

导入完成之后,我们就可以对smslib包进行二次开发了

下面我提供一段使用smslib包发送短信的代码

java 复制代码
import org.smslib.*;
import org.smslib.modem.SerialModemGateway;

public class SMSSender {
    public static void main(String[] args) {
        // Configure the modem gateway
        SerialModemGateway gateway = new SerialModemGateway("modem", "COM1", 9600, "", "");
        gateway.setInbound(true);
        gateway.setOutbound(true);

        try {
            // Initialize the SMSLib library
            Service.getInstance().addGateway(gateway);
            Service.getInstance().startService();

            // Create the outbound message
            OutboundMessage message = new OutboundMessage("recipientPhoneNumber", "你好世界");
            message.setEncoding(Message.MessageEncodings.ENCUCS2); // 设置编码格式为Unicode

            // Send the message
            Service.getInstance().sendMessage(message);

            // Clean up and stop the service
            Service.getInstance().stopService();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

上面代码中,如果需要使用到手机通讯模块发送中文,记得要加上

java 复制代码
message.setEncoding(Message.MessageEncodings.ENCUCS2);

这样一段代码才能正确设置编码格式

其次是这段代码

java 复制代码
SerialModemGateway gateway = new SerialModemGateway("modem", "COM1", 9600, "", "");

对于这个构造方法,我们需要传入的是gatewayID,smslib里面有一个任务队列,这个id需要不重复,其次是COM口和波特率,最后两项分别是设备厂商和型号,比如传入Nokia 和6130即可以 Nokia 6130设备的AT命令操作设备。一般来说这两项传空即可。

可以看到,源码中如果传的是空串,那么它会使用通用的athandler

可以看到,smslib包封装了一个同样athandler,同时,有些at命令稍有不同的设备也的方法进行了重写

从这里,我们可以得到什么?

拿到一个手机通讯模块,如果他们at指令是通用的,那么new SerialModemGateway 的时候,后两项为空就行,如果比如拿到了一个华为设备,只需要在构造对象的时候传入具体的厂商和型号就行,它会在AATHandler load的时候去反射拿到具体的指令细节。同时,如果我们手上的设备既有一些跟常见at命令不一样的设备,然后smslib包又没有进行扩展,我们便可以根据上面的ATHandler_Hauwei一样,自己继承Athandler,自己进行扩展

上面说完了发送短信,接下来我们还需要的是实现收短信的功能

java 复制代码
    /**
     * Java的smslib包下的IInboundMessageNotification接口用于定义回调方法,当短信网关接收到新的短信消息时,会调用这个方法。这个接口被实现的类需要在接收到新消息时被通知。
     */
    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("InboundNotification里面的gettext---------"+msg.getText());
            System.out.println(msg);
            try {
                // Uncomment following line if you wish to delete the message upon arrival.
                Service.getInstance().deleteMessage(msg);
            } catch (Exception e) {
                System.out.println("Oops!!! Something gone bad...");
                e.printStackTrace();
            }
        }
    }

只需要实现IInboundMessageNotification 里面的process方法即可,

然后从InboundMessage里面即可拿到短信的各项信息,然后进行业务处理

最后:smslib并没有编写手机通讯模块使用gprs功能发送数据的功能,我在它的基础进行了二次开发实现了网络通讯的功能,如果有需要的小伙伴可以私信我,我将无偿提供我的二开版本源码

相关推荐
能摆一天是一天1 小时前
JAVA stream().flatMap()
java·windows
CodeCraft Studio1 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一1 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
睡觉的时候不会困1 小时前
Redis 主从复制详解:原理、配置与主从切换实战
数据库·redis·bootstrap
颜如玉2 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员的世界你不懂3 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年3 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
自学也学好编程3 小时前
【数据库】Redis详解:内存数据库与缓存之王
数据库·redis
lingchen19063 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
gb42152874 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python