读书人

应用SMSLib发短信(收集)

发布时间: 2012-08-29 08:40:14 作者: rapoo

使用SMSLib发短信(收集)

1.mysql做后台数据库;

2.java应用smslib实现短信的发送与接收。

3.可以考虑在linux下实现;

?

SMSLib是一个API库用于通过GSM Modem收发SMS短信息。提供Java和.Net两个版本。

http://smslib.sourceforge.net/

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

1. 将win32com.dll放置在%JAVA_HOME%\jre\bin下

2. 将comm.jar放置在%JAVA_HOME%\jre\lib\ext下

3. 将javax.comm.properties放置在%JAVA_HOME%\jar\lib下

再试试SMSLib自带的examples,看看效果。

使用SMSLib发短信

最近的项目,有个需求是要发短信,设备是一台WAVECOM的串口短信 mao。

随猫附送的接口是ActiveX控件做的,我本来打算用VB写个接口程序来调用,但是看了下它的Demo例子,要发送的消息要放到控件的数组,发送成功以后还要自己删,相当麻烦。

调用短信 mao,都是通过串口通讯和AT命令,其实那个控件也不过是做了一下封装。在Java环境下,实现这些也是可以的,但是发中文短信要采用PDU模式,文字要经过PDU编码,去看文档也很麻烦。还好这些事情网上的开源项目SMSLib已经都已经帮我们做了。

到http://code.google.com/p/smslib下载了两个压缩文件smslib-v3.1.0.zip和javacomm20-win32.zip。

其中smslib-v3.1.0.zip是源代码,需要用ant编译成jar,javacomm20-win32.zip下是串行通讯协议需要的包。

javacomm20- win32.zip解开,里面的comm.jar需要放到%JAVA_HOME%/jre/lib/ext 下,javax.comm.properties放到%JAVA_HOME%/jre/lib下,win32com.dll放到%JAVA_HOME% /jre/bin下。路径放错了,调用起来就会报错的。

实际编译的时候,还需要一个commons-net的包,我下的是commons-net-1.4.1.jar。

环境配置复杂了点,但是用起来很简单,贴一下我的代码。

public boolean open() {

srv = new Service();

//comPort???? 串口名,比如COM1或者/dev/ttyS1

//baudRate?? 端口速度,WAVECOM是9600

//manufacturer,model 制造商和型号随便填

SerialModemGateway gateway = new SerialModemGateway("SMS",comPort,baudRate,manufacturer,model,srv.getLogger());

gateway.setInbound(true);

gateway.setOutbound(true);

//gateway.setSimPin("0000");

gateway.setOutboundNotification(outboundNotification);

srv.addGateway(gateway);

try {

?? srv.startService();

}

catch (Exception ex) {

?? log.error(ex);

?? return false;

}

return true;

}

public boolean sendSms(String mobile,String content) {

msg = new OutboundMessage(mobile, content);

msg.setEncoding(MessageEncodings.ENCUCS2);

try {

?? srv.sendMessage(msg);

?? System.out.println(msg);

}

catch (Exception ex) {

?? log.error(ex);

?? return false;

}

return true;

}

public void close() {

try {

?? srv.stopService();

}

catch (Exception ex) {

?? log.error(ex);

}

}

public class OutboundNotification implements IOutboundMessageNotification

{

public void process(String gatewayId, OutboundMessage msg)

{

?? System.out.println("Outbound handler called from Gateway: " + gatewayId);

?? System.out.println(msg);

}

}

另外附,通过远程终端按文本方式发短信的AT命令

AT+CMGF=1

OK

AT+CMGS=136XXXXXXX

>Test^Z

+CMGS: 204

OK

--------------------

JAVA短信收发控件开发示例

package com.diagcn.smslib.test;

import java.util.LinkedList;

import java.util.List;

import com.diagcn.smslib.CIncomingMessage;

import com.diagcn.smslib.CMessage;

import com.diagcn.smslib.COutgoingMessage;

import com.diagcn.smslib.CService;

import com.diagcn.smslib.CStatusReportMessage;

public class SampleClass {

/**

* @param args

*/

public static void main(String[] args) {

// 与短信设备建立连接,参数 1、端口号,2、速率,3、短信设备牌子,4、短信设备型号

CService srv = new CService("COM1", 9600, "Wavecom", "");

try {

// 设置短信中心号码

srv.setSmscNumber("+8613800210500");

// 连接设备

srv.connect();

// 连接成功,可以显示短信设备状态

System.out.println("Mobile Device Information: ");

System.out.println(" Manufacturer : "

+ srv.getDeviceInfo().getManufacturer());

System.out.println(" Model : "

+ srv.getDeviceInfo().getModel());

System.out.println(" Serial No : "

+ srv.getDeviceInfo().getSerialNo());

System.out.println(" IMSI : "

+ srv.getDeviceInfo().getImsi());

System.out.println(" S/W Version : "

+ srv.getDeviceInfo().getSwVersion());

System.out.println(" Battery Level : "

+ srv.getDeviceInfo().getBatteryLevel() + "%");

System.out.println(" Signal Level : "

+ srv.getDeviceInfo().getSignalLevel() + "%");

// 创建发送对象

COutgoingMessage msg = new COutgoingMessage("13917074111", "中文测试");

// 设置编码

msg.setMessageEncoding(CMessage.MessageEncoding.EncUcs2);

// 此短信需要状态回复

msg.setStatusReport(true);

// 短信有效期

msg.setValidityPeriod(8);

// 发送短信

srv.sendMessage(msg);

// 接收短信代码====================================================================

List<CIncomingMessage> msgList = new LinkedList<CIncomingMessage>();

srv.readMessages(msgList, CIncomingMessage.MessageClass.All);

for (int i = 0; i < msgList.size(); i++) {

CIncomingMessage message = msgList.get(i);

if (message instanceof CStatusReportMessage) {

// 此短消息为 状态回复短消息

}

srv.deleteMessage(message); // 删除都到的短信

}

// ==============================================================================

srv.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

}}

----------------------------------

使用java操作wavecom短信 mao来发短信的方法

2008年07月29日 星期二 09:58

由于业务的需要,今天用java实现了用wavecom短信 mao发短信的功能,本来这个应该用随猫购买的二次开发接口实现的,但由于这几台猫买的时候,经销商没有提供二次开发接口,所以我不得不在网上找了资料,自己写了个接口实现了发短信的功能。

?? 实现这个功能,需要两个jar包,可以到http://code.google.com/p/smslib下载smslib-v3.3.0-B2-bin.zip和javacomm20-win32.zip。

???? 首先,把smslib-v3.3.0-B2-bin.zip解开,在smslib\dist\lib目录下找到smslib-3.3.0b2.jar,放 入工程lib中,再把javacomm20-win32.zip解开,里面的comm.jar需要放到工程lib 下,javax.comm.properties放到%JAVA_HOME%/jre/lib下,win32com.dll放到%JAVA_HOME% /jre/bin下。路径放错了,调用起来就会报错的。

环境配置好了以后,使用起来很简单,贴下我的代码:

?? // SendMessage.java - Sample application.

//

// This application shows you the basic procedure for sending messages.

// You will find how to send synchronous and asynchronous messages.

//

// For asynchronous dispatch, the example application sets a callback

// notification, to see what's happened with messages.

package song.test;

import org.smslib.IOutboundMessageNotification;

import org.smslib.Library;

import org.smslib.OutboundMessage;

import org.smslib.Service;

import org.smslib.Message.MessageEncodings;

import org.smslib.modem.SerialModemGateway;

public class SendMessage

{

public void doIt() throws Exception

{

?? Service srv;

?? OutboundMessage msg;

?? OutboundNotification outboundNotification = new OutboundNotification();

?? System.out.println("Example: Send message from a serial gsm modem.");

?? System.out.println(Library.getLibraryDescription());

?? System.out.println("Version: " + Library.getLibraryVersion());

?? srv = new Service();

?

?? SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "17254");

?? gateway.setInbound(true);

?? gateway.setOutbound(true);

?? gateway.setSimPin("0000");

?? gateway.setOutboundNotification(outboundNotification);

?? srv.addGateway(gateway);

?? srv.startService();

?? 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();

?? // Send a message synchronously.

?

?? msg = new OutboundMessage("13649251175", "这个是用java发的中文短信!");//手机号码,和短信内容

?? msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的

?? srv.sendMessage(msg);

?? System.out.println(msg);

?? System.out.println("Now Sleeping - Hit <enter> to terminate.");

?? System.in.read();

?? srv.stopService();

}

public class OutboundNotification implements IOutboundMessageNotification

{

?? public void process(String gatewayId, OutboundMessage msg)

?? {

??? System.out.println("Outbound handler called from Gateway: " + gatewayId);

??? System.out.println(msg);

?? }

}

public static void main(String args[])

{

?? SendMessage app = new SendMessage();

?? try

?? {

??? app.doIt();

?? }

?? catch (Exception e)

?? {

??? e.printStackTrace();

?? }

}

}

读书人网 >开源软件

热点推荐