到http://smslib.org/download/下载安装文件
1.将comm.jar和smslib-3.5.1.jar放到web项目的WEB-INF下的lib目录;
2、将javax.comm.properties放到JDK安装的jre的lib目录中,例如E:\work\software\javahome\jre\lib;3、将libLinuxSerialParallel_g.so和libLinuxSerialParallel.so放到JDK安装的jre的bin目录下,例如E:\work\software\javahome\jre\bin
配置文件就写好了。你可以安装linux超级终端minicom(sudo apt-get install minicom安装),然后把gnokii装起来(sudo apt-get install gnokii)用AT命令发送一下短信,看自己是否配置正确。
1.发送短信主要代码
public void sendSMS(String number, String content) throws GatewayException, IOException {
String mobilePhones = "18312349876";
String content = "123yff";
System.out.println("hgj=sms##sendSMS##mobilePhones="+mobilePhones+",content="+content+"\n");
Service srv;
OutboundMessage msg;
srv = Service.getInstance();
srv.S.SERIAL_POLLING=true;//启用轮循模式
SerialModemGateway gateway = new SerialModemGateway("modem.com7", "/dev/ttyUSB1", 9600, "wavecom", "");//115200是波特率,一般为9600。可以通过超级终端测试出来
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
srv.addGateway(gateway);
System.out.println("hgj=sms##sendSMS##initService success!\n");
try {
srv.startService();
System.out.println("hgj=sms##sendSMS##startService success!\n");
String[] phones = mobilePhones.split(",");
for (int i = 0; i < phones.length; i++) {
msg = new OutboundMessage(phones[i], content);
msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
msg.setDate(new Date());
msg.setMessageStatus(OutboundMessage.MessageStatuses.SENT);
srv.sendMessage(msg);
System.out.println("hgj=sms##sendSMS##msg="+msg+"\n");
}
srv.stopService();
} catch (Exception e) {
e.printStackTrace();
}
}
2.接收短信主要代码
private Service srv;
public void acceptSMS() throws IOException, InterruptedException, SMSLibException {
if(srv!=null&&srv.getServiceStatus()!= ServiceStatus.STOPPED){ //该判断是为了重启
srv.stopService();
Collection<AGateway> smgList = srv.getGateways();
List<SerialModemGateway> ss = new ArrayList<SerialModemGateway>();
for(AGateway gateway:smgList){
SerialModemGateway smg = (SerialModemGateway)gateway;
ss.add(smg);
}
for(SerialModemGateway s :ss){
srv.removeGateway(s);
}
srv = null;
}
srv = Service.getInstance();// 创建服务
srv.S.SERIAL_POLLING=true;//启用轮循模式
// 实例化短信接入监听
InboundMessageNotification inboundMessageNotification = new InboundMessageNotification();
srv.setInboundMessageNotification(inboundMessageNotification);//添加短信监听
for(int i=5;i<8;i++){ //我们的设备有8个USB端口
// 设置相关参数(一般情况下以下默认参数不要变)
if(i==6){
continue;
}
SerialModemGateway gateway = new SerialModemGateway("modem.com"+i,"/dev/ttyUSB"+i, 9600, "wavecom", "");
// 设置短信接收状态为可接收
gateway.setInbound(true);
// 设置短信发送状态为可发送
gateway.setOutbound(true);
gateway.setSimPin("0000");
// 添加网关参数到服务
srv.addGateway(gateway);
}
try{
srv.startService();
System.out.println("acceptSMS##startService success!\n");
}catch (Exception e){
System.out.println("acceptSMS##startService exception!\n");
e.printStackTrace();
}
}
private class InboundMessageNotification implements IInboundMessageNotification
{
@SuppressWarnings("unchecked")
public void process(AGateway agw, MessageTypes messageTypes,InboundMessage message) {
List msgList;
String Originator = message.getOriginator(); //短信发送号码
if (messageTypes == MessageTypes.INBOUND) //收到短信
{
try
{
msgList = new ArrayList();
srv.readMessages(msgList, MessageClasses.ALL); // 读取短信
for (int i = 0; i < msgList.size(); i++){
InboundMessage acceptsms = (InboundMessage)msgList.get(i);
String smstime = sdf.format(acceptsms.getDate()); //接收短信的时间
Originator = acceptsms.getOriginator(); //电话号码
String text = acceptsms.getText(); //短信内容
System.out.println("acceptSMS##Originator="+Originator+",text="+text);
//srv.deleteMessage(acceptsms);// 插入成功后删除当前接收到的信息
}
}
catch (Exception e)
{
System.out.println("acceptSMS##srv.readMessages exception !\n");
e.printStackTrace();
}
}else if (messageTypes == MessageTypes.STATUSREPORT)
{
System.out.println("acceptSMS##dev exception : id = " +message.getGatewayId() );
}
}
}
参考文献:http://blog.sina.com.cn/s/blog_9f1c09310101a4i6.html