我正在尝试使用 D-Link USB 调制解调器在计算机上接收短信。我已经在这个链接上找到了我的问题的解决方案 但是现在我面临的问题是我收到了 3 次相同的消息,就像这样
New Inbound message detected from Gateway: 923145663675 Hello
New Inbound message detected from Gateway: 923145663675 Hello
New Inbound message detected from Gateway: 923145663675 Hello
此外,如果程序长时间保持打开状态,那么上面给出的这些行将一次又一次地打印在屏幕上已收到。代码如下
public void doIt() throws Exception{
InboundNotification inboundNotification = new InboundNotification();
try{
SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM7", 921600, "", "");
gateway.setProtocol(Protocols.PDU);
gateway.setInbound(true);
gateway.setSimPin("0000");
Service.getInstance().setInboundMessageNotification(inboundNotification);
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
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: " + msg.getOriginator() + " " + msg.getText());
try {
gateway.deleteMessage(msg);
} catch (GatewayException ex) {
Logger.getLogger(ReadMessages.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}