最近有个项目需要用到短信验证码,除了云平台提供的短信接口其实还可以用wavecom解决这个问题。
环境准备:
安装串口驱动:
安装成功后接入设备查看设备管理器:
pom文件依赖导入:
除了以上三个依赖还需要在Java的jdk->jre->bin目录下导入一个win32com.dll,这里jdk推荐1.5以上32位的(我的环境是1.8 ,32位)。
代码实现:
import lombok.extern.slf4j.Slf4j;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.util.*;
@Slf4j
public class SendMessage
{
public String doIt(String mobile,String text) throws Exception
{
Map<String, String> map = Read();
Service srv;
OutboundMessage msg;
// OutboundNotification outboundNotification = new ();
// System.out.println("Example: Send message from a serial gsm modem.");
// System.out.println(Library.getLibraryDescription());
// System.out.println("Version: " + Library.getLibraryVersion());
srv = new Service();
SerialModemGateway gateway = new SerialModemGateway(map.get("id"), map.get("comPort"), Integer.parseInt(map.get("baudRate")), map.get("manufacturer"), "");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
// gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
srv.startService();
// 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() + "%");
// System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
// System.out.println();
msg = new OutboundMessage(mobile, text);
msg.setEncoding(MessageEncodings.ENCUCS2);//中文短信
srv.sendMessage(msg);
// System.out.println(msg);
// System.out.println("Now Sleeping - Hit <enter> to terminate.");
srv.stopService();
return msg.toString();
}
// public static 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 Map<String,String> Read() throws IOException {
Map<String,String> map = new HashMap<>();
Properties properties = PropertiesLoaderUtils.loadAllProperties("sms.properties");
map.put("id",properties.getProperty("id"));
map.put("comPort",properties.getProperty("comPort"));
map.put("baudRate",properties.getProperty("baudRate"));
map.put("manufacturer",properties.getProperty("manufacturer"));
return map;
}
public static void main(String[] args)
{
SendMessage app = new SendMessage();
try
{
System.out.println(toMap(app.doIt("185****3412","测试短信")));
}
catch (Exception e)
{
e.printStackTrace();
log.error("发送验证码失败");
}
}
public static Map<Object,Object> toMap(String str){
Map<Object,Object> map = new HashMap<>();
String str1 =str.substring(str.indexOf("G")).substring(0,str.substring(str.indexOf("G")).indexOf("="));
List<Object> resultList = new ArrayList<>();
Object[] list = str1.split(": ");
for (Object o : list) {
List<Object> split = Arrays.asList(o.toString().split("\\r?\\n"));
resultList.addAll(split);
}
for (int i = 0; i < resultList.size(); i++) {
if (i%2==0){
Object key = resultList.get(i).toString().trim();
map.put(key,null);
}else{
Object key2 = resultList.get(i-1).toString().trim();
Object value = resultList.get(i).toString().trim();
map.put(key2,value);
}
}
return map;
}
}
我这里把串口配置做成了文件存储使用,以下是配置详情:
这里的comPort和baudRate需要换成你设备正在使用的配置:
图上框起来的两个参数可以手动修改为你想要的
如上改为你想发送给谁的手机号以及想发送的消息,然后启动这个main方法
如上控制台输出这段返回数据就代表发送成功了(我每次发都要等很久才有返回数据不知道为什么)