关于短信猫发送短信的问题
最近我在网上找了一个调用短信猫发送短信的程序,但在使用过程中,有时发送成功,有时发送不成功。错误如下:
org.smslib.GatewayException: GSM: Invalid CREG response.
at org.smslib.modem.AModemDriver.waitForNetworkRegistration(AModemDriver
.java:396)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:149)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)
at org.smslib.Service$1Starter.run(Service.java:227)
换了SIM卡也不行。请大家给指点,程序如下:
package com.serial;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org .smslib .IOutboundMessageNotification;
import org .smslib .OutboundMessage;
import org .smslib .Service ;
import org .smslib .Message.MessageEncodings;
import org .smslib .modem.SerialModemGateway;
/**
* 短信发送测试类
* @author mazq
*
*/
public class SMSUtil
{
public class OutboundNotification implements IOutboundMessageNotification
{
public void process(String gatewayId,OutboundMessage msg)
{
System.out.println( "Outbound handler called from Gateway: " + gatewayId);
System.out.println(msg);
}
}
public void sendSMS(String mobilePhones,String content)
{
//System.out.println(mobilePhones+"--"+content);
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
srv = new Service();
SerialModemGateway gateway = new SerialModemGateway( "modem.com1","COM3",9600,"wavecom","PL2302" );
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
System.out.println("初始化成功,准备开启服务");
try
{
srv.startService();
System.out.println("服务启动成功");
String[] phones = mobilePhones.split(",");
for(int i= 0;i<phones.length;i++)
{
msg = new OutboundMessage(phones[i],content); //手机号码,和短信内容
msg.setEncoding(MessageEncodings.ENCUCS2); //这句话是发中文短信必须的
srv.sendMessage(msg);
System.out.println(phones[i]+ " == " +content);
}
//srv.stopService();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
System.out.println("关闭服务......");
srv.stopService();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
SMSUtil util = new SMSUtil();
util.sendSMS("137XXXXXXXX","测试短信");
}
}