2012-03-09 60 views
3

我想發短信與smslib,但它沒有發送消息,有人可以指導我呢?smslib不發送短信爲什麼?

這是我的代碼:

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

和我的結果:

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. 
+0

+52似乎表明您將該郵件發送給墨西哥號碼。根據消息來源的評論,你是否改變了SMSC號碼?或者你是否真的擁有希臘沃達豐SIM卡? (雖然+91似乎暗示印度......) – andri 2012-03-09 22:49:39

+0

其實我改變了smsc根據一個程序,我的調制解調器GSM,程序打印模型的名稱和一切,只是不發送短信 – bentham 2012-03-10 23:51:04

+0

你已經成功從提供的軟件中發送帶有該棒的短信 - 以防硬件或SIM卡上的功能被禁用? – 2012-03-11 22:04:30

回答

5

這個例子有大約SMSC號碼碼額外的行。我玩過這個相同的庫,在我的代碼中沒有任何SMSC - 在我的代碼的任何一行。

這是一個建議,「如果需要」,我當然相信擺脫它可以解決您的問題。你很可能不知道你到底需要放什麼,所以最好不要離開它。然後,調制解調器不會嘗試手動執行此路由到給定的號碼,但它可以使它正確,它通過SIM卡上的SIM設置瞭解它。

我會檢查的另一件事是調制解調器真的從COM4端口回答。儘管現在似乎這樣做了,因爲信號強度被讀取。但請始終檢查,因爲服務器的每次啓動都可以將設備映射到不同的端口。我在Linux方面至少有這種問題。

1

也許您還沒有足夠小心(還)到SerialModemGateway構造函數的參數,因爲您離開「華爲」作爲供應商,而您使用諾基亞設備。該參數不重要,但波特率是。根據SMSlib documentation,大多數設備只能以預置/ uniq波特率正常工作。

我建議你打開其他軟件設置,以獲得或確認參數已使用:

  • 波特率
  • 網關SMSC號 - 也許從連接歷史記錄菜單根據諾基亞用戶指南

當你從一個華爲例子中得到你的代碼時,這個例子設置了網關SMSC號碼,但是這個參數對大多數設備是可選的,只有華爲設備可能需要它。嘗試運行沒有gateway.setSmscNumber

我還邀請您以Portmon爲例來監測串行端口流量,並在此處以及在SMSlib論壇上報告以獲得支持。

最後,你應該問SMSlib維護你的設備的選擇,因爲它是在compatibility list(還)

0

以下是我使用和測試的樣本代碼。您可以重新使用它。

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

我嘗試了您的代碼,並且收到了此異常java.lang.UnsupportedClassVersionError:org/smslib/IOutboundMessageNotification:不受支持的major.minor版本51.0 – 2014-09-03 07:05:57

+0

看起來好像某些組件之間存在版本不匹配,我不能確切地說,但請重新檢查是否使用相同jar和dll的源代碼。 – 2014-10-16 14:54:15