2012-08-29 15 views
0

使用SMSlib库从USB 3G调制解调器ZTE MF180发送短信。 我试图使用SendMessage.java类来测试我的调制解调器,所以我复制短信发送代码 - 所以理论上我期望得到2短信。SMSLib。 Service.getInstance()。stopService()不起作用

OutboundNotification outboundNotification = new OutboundNotification(); 
    SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM6", 115200, "ZTE", "MF180");   
    gateway.setInbound(true); 
    gateway.setOutbound(true); 
    gateway.setSimPin(""); 
    gateway.setSmscNumber("+79037011111"); 
    Service.getInstance().setOutboundMessageNotification(outboundNotification); 
    Service.getInstance().addGateway(gateway); 
    Service.getInstance().startService(); 
    OutboundMessage msg = new OutboundMessage("79213533296", "Hello world!"); 
    Service.getInstance().sendMessage(msg); 
    Service.getInstance().removeGateway(gateway); 
    Service.getInstance().stopService(); 

OutboundNotification outboundNotification2 = new OutboundNotification();

SerialModemGateway gateway2 = new SerialModemGateway("modem.com1", "COM6", 115200, "ZTE", "MF180"); 
    gateway2.setInbound(true); 
    gateway2.setOutbound(true); 
    gateway2.setSimPin(""); 
    gateway2.setSmscNumber("+79037011111"); 
    Service.getInstance().setOutboundMessageNotification(outboundNotification2); 
    Service.getInstance().addGateway(gateway2); 
    Service.getInstance().startService(); 
    OutboundMessage msg2 = new OutboundMessage("79213533296", "Оповещение о событии "); 
    Service.getInstance().sendMessage(msg2); 
    Service.getInstance().stopService(); 

我得到的第一个短信,然后落在例外:

org.smslib.GatewayException:通讯库异常:了java.lang.RuntimeException:gnu.io.PortInUseException:org.smslib 看来,像Service.getInstance()。stopService()方法不起作用。但我不知道该怎么做。

+0

我很困惑“Service.getInstance()。setOutboundMessageNotification()”如果你是从不同的线程发送消息?我想你只会做一次设置,并保持服务运行... –

回答

0

Your're试图创建一个新的SerialModemGateway到同一个COM端口,这是不可行的 - 只有一个进程/服务可以有一个COM端口同时打开。

确保你停止做gateway.stopGateway()

创建另一个之前的第一个虽然你并不真的需要建立和拆除SerialModemGateway和停止/每次启动该服务,只需创建一个新的消息并使用您打开的现有网关和服务发送。

+0

如何更新网关设置,Incase如果我修改了端口或制造商的网关设置? – mreaevnia