3

我正在尝试将我的手机用作 GSM 调制解调器。我使用 SMSLib 通过此调制解调器发送和接收 SMS。问题是,当我的手机(GSM 调制解调器)收到短信时,我不会通过 SMSLib 通知。但代码整体很好,例如当 GSM 调制解调器接到电话时会通知我。我的代码没有任何错误,因为我只使用 SMSLib 示例代码来接收消息。SMSLib 示例代码是:

public class TestSinaRec
{
    public void doIt() throws Exception
    {
        // Define a list which will hold the read messages.
        List<InboundMessage> msgList;
        // Create the notification callback method for inbound & status report
        // messages.
        InboundNotification inboundNotification = new InboundNotification();
        // Create the notification callback method for inbound voice calls.
        CallNotification callNotification = new CallNotification();
        //Create the notification callback method for gateway statuses.
        GatewayStatusNotification statusNotification = new GatewayStatusNotification();
        OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();
        try
        {
            System.out.println("Example: Read messages from a serial gsm modem.");
            System.out.println(Library.getLibraryDescription());
            System.out.println("Version: " + Library.getLibraryVersion());
            // Create the Gateway representing the serial GSM modem.
            SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM4", 115200, "Nokia", " 6303i");
            // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
            gateway.setProtocol(Protocols.PDU);
            // Do we want the Gateway to be used for Inbound messages?
            gateway.setInbound(true);
            // Do we want the Gateway to be used for Outbound messages?
            gateway.setOutbound(true);
            // Let SMSLib know which is the SIM PIN.
            gateway.setSimPin("0444");
            // Set up the notification methods.
            Service.getInstance().setInboundMessageNotification(inboundNotification);
            Service.getInstance().setCallNotification(callNotification);
            Service.getInstance().setGatewayStatusNotification(statusNotification);
            Service.getInstance().setOrphanedMessageNotification(orphanedMessageNotification);
            // Add the Gateway to the Service object.
            Service.getInstance().addGateway(gateway);
            // Similarly, you may define as many Gateway objects, representing
            // various GSM modems, add them in the Service object and control all of them.
            // Start! (i.e. connect to all defined Gateways)
            Service.getInstance().startService();
            // Printout some general information about the modem.
            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();
            // In case you work with encrypted messages, its a good time to declare your keys.
            // Create a new AES Key with a known key value. 
            // Register it in KeyManager in order to keep it active. SMSLib will then automatically
            // encrypt / decrypt all messages send to / received from this number.
            //Service.getInstance().getKeyManager().registerKey("+306948494037", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));
            // Read Messages. The reading is done via the Service object and
            // affects all Gateway objects defined. This can also be more directed to a specific
            // Gateway - look the JavaDocs for information on the Service method calls.
            msgList = new ArrayList<InboundMessage>();
            Service.getInstance().readMessages(msgList, MessageClasses.ALL);
            for (InboundMessage msg : msgList)
                System.out.println(msg);
            // Sleep now. Emulate real world situation and give a chance to the notifications
            // methods to be called in the event of message or voice call reception.
            System.out.println("Now Sleeping - Hit <enter> to stop service.");
            System.in.read();
            System.in.read();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            Service.getInstance().stopService();
        }
    }

    public class InboundNotification implements IInboundMessageNotification
    {
        public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg)
        {
            if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());
            else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
            System.out.println(msg);
        }
    }

    public class CallNotification implements ICallNotification
    {
        public void process(AGateway gateway, String callerId)
        {
            System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);
        }
    }

    public class GatewayStatusNotification implements IGatewayStatusNotification
    {
        public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus)
        {
            System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
        }
    }

    public class OrphanedMessageNotification implements IOrphanedMessageNotification
    {
        public boolean process(AGateway gateway, InboundMessage msg)
        {
            System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());
            System.out.println(msg);
            // Since we are just testing, return FALSE and keep the orphaned message part.
            return false;
        }
    }

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

程序输出例如:

modem.com4 的网关状态更改,旧:已停止 -> 新:正在启动 modem.com4 的网关状态更改,旧:正在启动 -> 新:已启动

调制解调器信息: 制造商:Nokia 型号:Nokia 6303i classic 序列号:35538​​2041051833 SIM IMSI:** MASKED ** 信号电平:-57 dBm 电池电量:91%

Now Sleeping - 点击停止服务。从网关检测到新呼叫:modem.com4:+989111007483 从网关检测到新呼叫:modem.com4:+989111007483

当我搜索这个问题时,我发现了这个:

此方法的正确操作取决于主动提供的调制解调器指示和 CNMI 命令的正确操作。如果您发现使用回调方法无法接收消息,则可能是调制解调器指示设置不正确。

所以我用诺基亚 6303i 而不是我首先使用的诺基亚 5200 更换了我的手机(我的 GSM 调制解调器),但问题没有解决。所以现在我真的不知道选择其他手机会解决这个问题吗?!或者我应该寻找更好、更合理的解决方案。

感谢您为解决此问题提供的任何帮助。

4

3 回答 3

0

那么我唯一能想到的就是你正在启动Service然后SMS发送到调制解调器。因此,不会调用此行:Service.getInstance().readMessages(msgList, MessageClasses.ALL);. 但是,您仍然应该收到新消息已到达调制解调器的通知。

InboundNotification当它在调制解调器上检测到任何新消息时,尝试实现以获取消息。通过覆盖该process()方法来做到这一点。

但是,这也可能是因为您实际上按<Enter>得太早了。正如评论所说;你必须等待去给通知方法一个被调用的机会。

有时它就是这么愚蠢的事情。让我知道它是否有帮助,或者我是否完全误解了您的问题。我自己正在开发一个多调制解调器网关,所以我很乐意提供帮助。

于 2012-09-12T12:39:52.917 回答
0


我遇到了 GT-I9000 的问题,他收到了入站警报,但无法获取正确的短信对象,我认为这是存储位置的问题,我尝试使用另一部手机(三星 GT-S5670 Android)我的朋友在 SIM 卡内存中存储了一些消息,通知了 smslibrary 并且 ReadMessages 类记录了所有消息。

所以我认为您需要找到某种方式来更改 ReadMessages.java 上的存储位置,或者找到可以将短信存储到 SIM 卡而不是手机内存的兼容手机。

希望这有帮助。

于 2012-12-11T16:16:44.667 回答
0

问题出在我的手机上。Smslib 不适用于各种手机(包括智能手机、大多数诺基亚手机等)的收听短信。我没有检查,但如果你使用专用的可能会解决这个问题GSM调制解调器(如华为GSM调制解调器)

于 2014-07-18T17:20:42.220 回答