金笛工业手机论坛

金笛工业手机论坛 (http://bbs.sendsms.cn/)
-   短信开发资料 (http://bbs.sendsms.cn/cy-ae-eu-ae/)
-   -   Windowsxp下短信接收出现的问题,求解决。。。 (http://bbs.sendsms.cn/cy-ae-eu-ae/2527.htm)

Jeffymail2008 2012-05-23 22:26

Windowsxp下短信接收出现的问题,求解决。。。
 
我在Windows XP下实现短信接收,代码是用的下载包中的ReadMessages.java。
代码如下:
代码:

// JindiSMS v3.1
// 该程序主要用于读取GSM Modem接收到的短信。
// 金笛短信网 www.sendsms.cn
// 为节省您的开发时间,请配合金笛短信设备以达到最佳效果。
package test;

import java.util.*;
import cn.sendsms.*;

public class ReadMessages {
        private Service srv;

        public void doIt() throws Exception {
                List msgList;
                InboundNotification inboundNotification = new InboundNotification();
                CallNotification callNotification = new CallNotification();
                try {
                        System.out.println("示例: 从串口短信设备读取短信.");
                        System.out.println(Library.getLibraryDescription());
                        System.out.println("版本: " + Library.getLibraryVersion());
                        srv = new Service();
                        // 创建串口GSM modem连接通道.
                        SerialModemGateway gateway = new SerialModemGateway("jindi", "COM3", 115200, "wavecom", "M1306B", srv.getLogger());
                        // 设置通道是否接收短信
                        gateway.setInbound(true);
                        // 设置通道是否可以发送短信
                        gateway.setOutbound(true);
                        gateway.setSimPin("0000");
                        // 设置短信到达后调用方法
                        gateway.setInboundNotification(inboundNotification);
                        gateway.setCallNotification(callNotification);
                        // 增加短信通道到服务对象
                        srv.addGateway(gateway);
                        // 如果有多个设备,可以依次添加到服务对象。

                        // 启动服务! (连接到所有已定义的设备通道)
                        srv.startService();
                        System.out.println();
                        System.out.println("GSM Modem 信息:");
                        System.out.println("  厂家: " + gateway.getManufacturer());
                        System.out.println("  型号: " + gateway.getModel());
                        System.out.println("  序列号: " + gateway.getSerialNo());
                        System.out.println("  SIM IMSI: " + gateway.getImsi());
                        System.out.println("  信号强度: " + gateway.getSignalLevel() + "%");
                        System.out.println("  电池容量: " + gateway.getBatteryLevel() + "%");
                        System.out.println();
                        // 读取短信.
                        msgList = new ArrayList();
                        srv.readMessages(msgList, MessageClasses.ALL);
                        for (int i = 0; i < msgList.size(); i++) {
                                InboundMessage inMsg = (InboundMessage)msgList.get(i);
                                System.out.println(inMsg.toString());
                        }
                        // 进入休眠状态. 如果有新短信进来,就会重新激活。
                        System.out.println("进入等待状态... - 按 <enter>回车键退出.");
                        System.in.read();
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        srv.stopService();
                }
        }

        public class InboundNotification implements IInboundMessageNotification {
                public void process(String gatewayId, MessageTypes msgType,
                                String memLoc, int memIndex) {
                        List msgList;
                        System.out.println("received a message, msgType=" + msgType);
                        if (msgType == MessageTypes.INBOUND) {
                                System.out.println(">>> 监测到设备收到新的短信: " + gatewayId + " : "
                                                + memLoc + " @ " + memIndex);
                                try {
                                        // Read...
                                        msgList = new ArrayList();
                                        srv.readMessages(msgList, MessageClasses.UNREAD, gatewayId);
                                        for (int i = 0; i < msgList.size(); i++)
                                                System.out.println(msgList.get(i));
                                        // ...and reply.
                                        // for (int i = 0; i < msgList.size(); i ++)
                                        // srv.sendMessage(new
                                        // OutboundMessage(msgList.get(i).getOriginator(),
                                        // "Got it!"), gatewayId);
                                } catch (Exception e) {
                                        System.out.println("有异常...");
                                        e.printStackTrace();
                                }
                        } else if (msgType == MessageTypes.STATUSREPORT) {
                                System.out.println(">>> 监测到设备收到短信状态报告: " + gatewayId + " : "
                                                + memLoc + " @ " + memIndex);
                        }
                }
        }

        public class CallNotification implements ICallNotification {
                public void process(String gatewayId, String callerId) {
                        System.out.println(">>> 监测到有电话打入: " + gatewayId + " : " + callerId);
                }
        }

        public static void main(String args[]) {
                ReadMessages app = new ReadMessages();
                try {
                        app.doIt();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

其中,
代码:

srv.readMessages(msgList, MessageClasses.ALL);
上面收取短信的语句貌似不稳定,有时候能拿到短信,但拿到的不是所有的短信,未读的短信拿不到,并且不能收到新短信,即InboundNotification没有触发;有时候直接报一下错:
代码:

cn.sendsms.TimeoutException: No response from device.
        at cn.sendsms.ModemDriver$CharQueue.get(ModemDriver.java:388)
        at cn.sendsms.ModemDriver.getResponse(ModemDriver.java:202)
        at cn.sendsms.ATHandler.getSimStatus(ATHandler.java:71)
        at cn.sendsms.ModemDriver.connect(ModemDriver.java:89)
        at cn.sendsms.ModemGateway.startGateway(ModemGateway.java:76)
        at cn.sendsms.Service.startService(Service.java:114)
        at SendMessage.doIt(SendMessage.java:32)
        at SendMessage.main(SendMessage.java:82)

又有时候报一下错误:
代码:

0 [main] ERROR org.smslib  - *****
0 [main] ERROR org.smslib  - Unhandled SMS in inbox, skipping!
0 [main] ERROR org.smslib  - Err: For input string: "
0 [main] ERROR org.smslib  - *****

51516 [Thread-0] ERROR org.smslib  - ModemDriver: KeepAlive Error: No response from device.

这到底是什么问题,甚是奇怪。。。

Jeffymail2008 2012-05-24 10:47

这里的论坛貌似不活跃啊。。。

水哭了 2012-05-28 16:53

请加QQ:1532322176 WIN API3.1在接收短信的时候,有时候会出现异常,请下载API3.4的API包,可以解决这类的问题。如果还有疑问请加我QQ,我可以给我传一下及远程操作协助等。


所有时间均为北京时间。现在的时间是 17:36

Powered by vBulletin® 版本 3.8.3
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.