java at sms_Java调用 SMS Cat发短信

居然不能发表SMS Cat的中文名?!

这个是 SMS Cat设备

442d5f02951f3b5e2cb49b9b525c6b62.png

需要插入SIM卡,所以,是需要付短信费的。

插好USB和电源后,会安装好驱动,可以在设备管理可以查看到端口号

981f1060a56a7db52b655c0acf83ba65.png

也有一种软件可以测试端口是否可连接:secureCRT

77f912a254c484b9376410cd62da1ff9.png

新建好connection后,输入AT测试,如果OK就OK。

ced1887e2c67f362a1fe74d7f516212d.png

好了,端口确定没问题了。

接着,在你的JDK的bin路径下放一个win32com.dll

6a2ee0e889cf819b8d210c0c314a4f6f.png

在JDK的lib中放一个comm.jar和javax.comm.properties

98bf1a2fc55c27d97de236b9d85f9df8.png

需要的文件都在附件SMSCat.rar中。

OK,所有都准备完了,现在开始代码测试。

demo测试需要用到的jar

c11f4e7861949525f66ef7f218e841eb.png

sms.properties#sms properties

Message.comId=modem.com19

Message.com=COM19

Message.baudRate=9600

Message.manufacturer=wavecom

Message.model=

Message.simPin=0000

注意:这里的端口COM19必须和你上面的端口对应。

SMSService.java

/**

* Copyright(C) 2012 GZ ISCAS ALL Rights Reserved

*/

package com.royal.SMSCat;

import java.util.Properties;

import org.smslib.Message.MessageEncodings;

import org.smslib.OutboundMessage;

import org.smslib.Service;

import org.smslib.modem.SerialModemGateway;

import com.royal.utils.PropertiesUtil;

/**

* 描述:SMS Cat服务类

*/

public class SMSService {

/**

* 私有静态实例

*/

private static SMSService instance = null;

/**

* 是否开启服务

*/

private boolean isStartService = false;

/**

* 私有构造方法

*/

private SMSService() {

}

/**

* 获取实例(单例模式)

*

* @return

*/

public static SMSService getInstance() {

if (instance == null) {

instance = new SMSService();

}

return instance;

}

/**

* 开启短信服务

*

* @param path

* 配置文件路径

*/

public void startService(String path) {

System.out.println("开始初始化SMS服务!");

// 加载文件属性

Properties p = null;

try {

p = PropertiesUtil.getProperties(path);

} catch (Exception e) {

System.out.println("加载属性文件出错:" + e.getMessage());

return;

}

// 初始化网关,参数信息依次为:COMID,COM号,比特率,制造商,Modem模式

SerialModemGateway gateway = new SerialModemGateway(p.getProperty("Message.comId"), p.getProperty("Message.com"), Integer.parseInt(p.getProperty("Message.baudRate")), p.getProperty("Message.manufacturer"), p.getProperty("Message.model"));

gateway.setInbound(true);

gateway.setOutbound(true);

gateway.setSimPin(p.getProperty("Message.simPin"));

OutboundNotification outboundNotification = new OutboundNotification();

Service service = Service.getInstance();

if (service == null) {

System.out.println("初始化SMS服务失败!");

return;

}

service.setOutboundMessageNotification(outboundNotification);

try {

service.addGateway(gateway);

// 开启服务

service.startService();

System.out.println("初始化SMS服务成功!");

isStartService = true;

} catch (Exception e) {

System.out.println("开启SMS服务异常:" + e.getMessage());

}

}

/**

* 停止SMS服务

*/

public void stopService() {

try {

Service.getInstance().stopService();

} catch (Exception e) {

System.out.println("关闭SMS服务异常:" + e.getMessage());

}

isStartService = false;

}

/**

* 发送短信

*

* @param toNumber

* 手机号码

* @param message

* 短信内容

*/

public void sendMessage(String toNumber, String message) {

if (!isStartService) {

System.out.println("尚未开启SMS服务!");

return;

}

// 封装信息

OutboundMessage msg = new OutboundMessage(toNumber, message);

msg.setEncoding(MessageEncodings.ENCUCS2);

try {

// 发送信息

Service.getInstance().sendMessage(msg);

} catch (Exception e) {

System.out.println("SMS服务发送信息发生异常:" + e.getMessage());

isStartService = false;

}

}

}

OutboundNotification.java

package com.royal.SMSCat;

import org.smslib.AGateway;

import org.smslib.IOutboundMessageNotification;

import org.smslib.OutboundMessage;

/**

* 封装发送短信类

*/

public class OutboundNotification implements IOutboundMessageNotification {

public void process(AGateway gateway, OutboundMessage msg) {

System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());

}

}

SMSCatClient.java

package com.royal.SMSCat;

public class SMSCatClient {

/**

* 测试

*

* @param args

*/

public static void main(String[] args) {

String path = "D:\\sms.properties";

SMSService.getInstance().startService(path);

SMSService.getInstance().sendMessage("13800138000", "测试 Test!");

//没必要的时候没停止服务,因为端口占用着

SMSService.getInstance().stopService();

}

}

测试结果自己找个手机号测吧

4e559b4cf56fd042cc7c2c93f0b71853.png

看见了吗?控制台中的服务(红色标识)还在跑着,也就是端口还在占用着;服务没断,可以不用重新初始化。

fd21080ae15ed436478bd540272f1d98.png

大小: 1 MB

043b5f19bf853ac7f615c7e7967718e2.png

大小: 14.4 KB

a53a86e5d3c065141663ce46a4b19b9a.png

大小: 6.7 KB

e6ccc401dcad6067ad1f53a0d1bf80aa.png

大小: 53.8 KB

adc370a1aee2c88fd88ac5ad820bb1f5.png

大小: 17.1 KB

c64319193a23c82e7c185b7fbd9d39c8.png

大小: 16.7 KB

8b32a8d6174f0ed026faf2a6ff23715c.png

大小: 64.4 KB

2e01aedcf89f16dbdc70d7f83b857900.png

大小: 14.6 KB

下载次数: 265

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-11-02 16:38

浏览 1864

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值