首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么不发短信?

为什么不发短信?
EN

Stack Overflow用户
提问于 2012-03-10 03:19:42
回答 3查看 7.3K关注 0票数 4

我正在尝试发送一条短信与smslib,但它没有发送的消息,有人可以指导我在这?

这是我的代码:

代码语言:javascript
复制
import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class SendMessage
{
    public void doIt() throws Exception
    {
        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());
        SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM4", 115200, "Huawei", "");
        gateway.setInbound(true);
        gateway.setOutbound(true);
        gateway.setSimPin("0000");
        // Explicit SMSC address set is required for some modems.
        // Below is for VODAFONE GREECE - be sure to set your own!
        gateway.setSmscNumber("+919825068000");
        Service.getInstance().setOutboundMessageNotification(outboundNotification);
        Service.getInstance().addGateway(gateway);
        Service.getInstance().startService();
        System.out.println();
        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() + " dBm");
        System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
        System.out.println();
        // Send a message synchronously.
        OutboundMessage msg = new OutboundMessage("+524747388616", "que onda como andas!");
        Service.getInstance().sendMessage(msg);
        System.out.println(msg);
        // Or, send out a WAP SI message.
        //OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("306974000000",  new URL("http://www.smslib.org/"), "Visit SMSLib now!");
        //Service.getInstance().sendMessage(wapMsg);
        //System.out.println(wapMsg);
        // You can also queue some asynchronous messages to see how the callbacks
        // are called...
        //msg = new OutboundMessage("309999999999", "Wrong number!");
        //srv.queueMessage(msg, gateway.getGatewayId());
        //msg = new OutboundMessage("308888888888", "Wrong number!");
        //srv.queueMessage(msg, gateway.getGatewayId());
        System.out.println("Now Sleeping - Hit <enter> to terminate.");
        System.in.read();
        Service.getInstance().stopService();
    }

    public class OutboundNotification implements IOutboundMessageNotification
    {
        public void process(AGateway gateway, OutboundMessage msg)
        {
            System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());
            System.out.println(msg);
        }
    }

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

我的结果是:

代码语言:javascript
复制
Example: Send message from a serial gsm modem.
SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways.
This software is distributed under the terms of the Apache v2.0 License.
Web Site: http://smslib.org
Version: 3.5.1

Modem Information:
  Manufacturer: Nokia Corporation
  Model: Nokia Internet Stick CS-10
  Serial No: 359340022861915
  SIM IMSI: ** MASKED **
  Signal Level: -53 dBm
  Battery Level: 0%


===============================================================================
<< OutboundMessage >>
-------------------------------------------------------------------------------
 Gateway Id: *
 Message Id: 0
 Message UUID: e30f84ad-b083-4956-85ef-16dc89020769
 Encoding: 7-bit
 Date: Fri Mar 09 13:15:52 CST 2012
 SMSC Ref No: null
 Recipient: 524747388616
 Dispatch Date: null
 Message Status: FAILED
 Failure Cause: UNKNOWN
 Validity Period (Hours): -1
 Status Report: false
 Source / Destination Ports: -1 / -1
 Flash SMS: false
 Text: que onda como andas!
 PDU data: F17A19F47693C3A0F1BBFD0685DDE4F03C04
 Scheduled Delivery: null
===============================================================================

Now Sleeping - Hit <enter> to terminate.
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-13 03:30:23

这个例子有额外的一行关于SMSC-number的代码。我使用过这个库,在我的代码中没有任何SMSC -在我的代码的任何一行。

这是一个建议,“如果需要”,我当然相信摆脱它可以解决你的问题。你很可能不知道你到底要在上面放些什么,所以最好还是把它去掉吧。则调制解调器将不会尝试手动路由到给定号码,但是它可以通过SIM卡上的SIM卡设置使其到达正确的、它所知道的。

我要检查的另一件事是调制解调器是否真的从COM4端口应答。尽管现在它似乎是这样做的,因为信号强度是读取的。但是一定要检查这一点,因为服务器的每次启动都可以将设备映射到不同的端口。我至少在Linux方面遇到了这样的问题。

票数 6
EN

Stack Overflow用户

发布于 2012-03-13 04:58:20

也许你还没有足够注意到SerialModemGateway构造函数的参数,因为你留下了“华为”作为供应商,而你使用的是诺基亚设备。该参数并不重要,但波特率很重要。根据SMSlib documentation的说法,大多数设备只能在预设/uniq波特率下正常工作。

我建议您打开其他软件设置来获取或确认您使用的参数:

  • 波特率
  • 网关短消息中心号码-根据诺基亚用户指南

,可能来自连接历史菜单

正如您从华为示例中获得的代码一样,此示例设置了网关SMSC编号,但该参数对于大多数设备来说都是可选的,只有华为设备可能需要它。尝试在没有gateway.setSmscNumber的情况下运行!

例如,我还邀请您使用Portmon监控串行端口流量,并在此处和SMSlib论坛上报告,以获得支持。

最后,您应该向SMSlib maintainer询问有关您的设备的选项,因为它在compatibility list中(还没有)

票数 1
EN

Stack Overflow用户

发布于 2013-06-23 21:42:14

以下是我使用和测试的示例代码。你可以重复使用它。

代码语言:javascript
复制
package com.stackoverflow.smstest;

import java.net.URL;

import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.OutboundWapSIMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class Main {

    public void sendMessage() throws Exception {
        OutboundNotification outboundNotification = new OutboundNotification();
        System.out.println("Sample of Send message from a serial gsm modem.");
        System.out.println(Library.getLibraryDescription());
        System.out.println("Version: " + Library.getLibraryVersion());
        SerialModemGateway gateway = new SerialModemGateway("modem.com4",
                "COM4", 57600, "Huawei", "E160");
        gateway.setInbound(false);
        gateway.setOutbound(true);
        // gateway.setSimPin("");
        Service.getInstance().setOutboundMessageNotification(
                outboundNotification);
        Service.getInstance().addGateway(gateway);
        Service.getInstance().startService();
        System.out.println();
        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()
                + " dBm");
        System.out.println("  Battery Level: " + gateway.getBatteryLevel()
                + "%");

        // Send a message synchronously.
        OutboundMessage msg = new OutboundMessage("+94123456789",
                "SMS test: sample message from StackOverflow");

        Service srvice = Service.getInstance();
        // Service.getInstance().sendMessage(msg);
        System.out.println(msg);
        // Or, send out a WAP SI message.
        OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("+94123456789",
                new URL("http://stackoverflow.com/"),
                "WAP test: sample message from StackOverflow!");
        // gateway.setFrom("chandpriyankara");
        // wapMsg.setFrom("chandpriyankara");
        srvice.queueMessage(wapMsg);

        Service.getInstance().stopService();
    }

    /**
     * Outbound Message informations handler
     * 
     * @author chandpriyankara
     * 
     */
    public class OutboundNotification implements IOutboundMessageNotification {
        public void process(AGateway gateway, OutboundMessage msg) {
            System.out.println("Outbound handler called from Gateway: "
                    + gateway.getGatewayId());
            System.out.println(msg);
        }
    }

    public static void main(String args[]) {
        Main app = new Main();
        try {
            app.sendMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9639750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档