随笔-9  评论-168  文章-266  trackbacks-0

前言:
JAVA发送SMS短信有两种方法:一是通过运营商的网关;二是通过短信猫,不是很复杂(本文主要介绍)。

一、SMS短信发送方式(2种)
1、在Java编程中可以通过Java Comm类进行手机与电脑的串口通讯,并通过AT指令控制手机操作。(最底层方式)
2、SMSLib项目,封装了底层AT指令集,不需自己在去写底层的 AT指令与手机进行通讯。(需要SMSLib开发包,没有完整的API文档,只能通过样例查看,其运行时需要slf4j(类似log4j)和Java comm的支持)

二、所需的Java comm 文件及配置方法:
(1)下载文件:javacomm20-win32.zip
JAVA_HOME%是jdk的路径,而非jre。SMSlib 只支持JDK1.6及以上.
1.comm.jar提供了通讯用的java API。拷贝到%JAVA_HOME%\jre\lib\ext目录。
2.win32com.dll提供了供comm.jar调用的本地驱动接口。拷贝到%JAVA_HOME%\bin
3.javax.comm.properties是这个驱动的类配置文件。复制到%JAVA_HOME%\jre\lib目录。
(2)下载SMSLib

三、开发步骤:
(1)创建并配置相关对象并启动服务

  // 创建服务对象。(仅且只有一个服务对象,但可以有多个网关)
   this.srv = new Service();

   // 创建一个GSM modem 类型代替网关(也可以创建别的类型)。
   SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 57600, "Nokia", "");
   gateway.setProtocol(Protocols.PDU);// 设置PDU协议(PDU是默认的,也可以设置为TEXT简单文本协议).
   gateway.setInbound(true);//设置网关能否接收消息(入站)
   gateway.setOutbound(true);//设置网关能否发送消息(出站)
   gateway.setSimPin("0000");// Let SMSLib know which is the SIM PIN.

   //对于异步处理短信时,才采用事件驱动机制,如下:
   //设置事件的回调类(以下仅包括一部分.每个事件都是一个实现对应接口的类并实现其process方法).
   this.srv.setInboundMessageNotification(inboundNotification);//收到短信事件函数
   this.srv.setCallNotification(callNotification);//接到电话事件函数
   this.srv.setGatewayStatusNotification(statusNotification);//网关状态变化事件函数
   this.srv.setOrphanedMessageNotification(orphanedMessageNotification);


   this.srv.addGateway(gateway);// 添加网关到服务对象中.
   this.srv.startService();// 启动服务对象.

(2)读取短信
同步读取示例:
   msgList = new ArrayList<InboundMessage>();
   //读取信息(主要方法)--两个参数指定要保存的位置和要读取的短信的类型,如已读,未读,信息报告等。
   this.srv.readMessages(msgList, MessageClasses.ALL);
   for (InboundMessage msg : msgList)
    System.out.println(msg);


There are two ways to read messages:

The synchronous way: just call the readMessages() method of the Service class. SMSLib will iterate all defined gateways, collect messages from all and return them in a collection. Note that you will be blocked until SMSLib returns. There are many forms of the readMessages() call - choose the one according to your needs.
The asynchronous way: you can implement some callback methods in your code (listeners) and pass them to the Service. SMSLib will then automatically call you upon message reception. For more information, see Callback methods.

同步读取:this.srv.readMessages(msgList, MessageClasses.ALL);
异步读取:在事件处理函数中读取.

(3)发送短信

There are two ways to send a message:

The synchronous way: create a message object and call the sendMessage() method of the Service class. Note that you will be blocked until SMSLib returns. Upon return, you can examine the message object to see the fate of your message.
The asynchronous way: use the queueMessage() family of methods to queue your message. Queueing a message returns control immediately to you. SMSLib keeps this message in internal queues and send its in the background. To learn about the fate of your message, you should setup a IOutboundMessageNotification callback method (see Callback methods).

同步发送:sendMessage()
异步发送:queueMessage()


名词解释:
gateway的概念的解释介绍:

       gateway是用来收发sms短信的一个设备或者一个服务的接口,一个网关可以是一个gsm modem 也可以是一个服务提供商,smslib 可以同时处理多个gateway

 

发送优先级:

Smslib 发送短信可以提供一个无限制的优先级,(就是发送多条短信时的一个策略,哪个先发,哪个后发),smslib 优先级策略是跟你的gateway(gsm modem 或者是短信提供商)对应的,除非他们是伪照的。

 

异步、同步接收

Smslib 有两个方式接收短信

1.       同步接收:你可以随时的调用read()方法来接收短信。

2.       异步接收:等待smslib 自动接收短信,当短信到来时。

 

异步、同步发送

Smslib 同样有两种方式去发送短信

1.       同步发送:当你的app使用smslib 发送一条短信时,app线程将会锁定,直到短信发送完毕,或是发送失败。

2.       异步发送:当你使用这种方式发送一条短信时,app 线程不会锁定,smsliib 会马上返回,smslib 会将你的短信(s)在后台发送。你可以选择是否提醒发出短信的状态。

 

回调函数

当有新事件发生时(收到新短信,或者是一条短信发送成功或者是失败),smslib 可以定义回调方法(事件驱动)

Inbound voice calls:当有一电话打进时,你可以设置是否提醒你,请注意这个呼叫将会自动的无条件的自动挂断

Inbound messages:每当有新的短信来到时,smlib 可以设置调用你的指定的方法

Outbound messages:当你使用异步发送信息时,你可以调用你指定的方法来得到已经发送的短信的状态信息等。

Gateway status changes:smslib 可以提醒你每次gateway 改变状态的情况

Queue sending operation:smslib 可以随时随刻的准备发送一条短信,可以随时随地的取到当前短信的发送状态。


 

posted on 2010-06-11 09:26 紫蝶∏飛揚↗ 阅读(7624) 评论(0)  编辑  收藏 所属分类: JAVA 串行通信

只有注册用户登录后才能发表评论。


网站导航: