二维码配网 (Two-dimensional code distribution network)
2.APP端检测是否打开定位权限获取WIFI名称,用户输入WIFI密码,获取到WIFI名称以及密码后保存进入下一步。(The APP detects whether the privilege is enabled to obtain the WIFI name. The user enters the WIFI password, obtains the WIFI name and password, and saves the next step.)
3.拼接生成二维码需要的字符串 (Stitching the string needed to generate the QR code)
String wifiString = new StringBuilder()
.append("S=").append(ssid).append(",")
.append("P=").append(pwd).append(",")
.append("A=").append("15111520684").append(",")
.append("U=").append("abc").append(",")
.append("T=").append(wifiType)
.toString();
Bitmap mBitmap = CodeUtils.createImage(wifiString, width, width, null);
ivQRcode.setImageBitmap(mBitmap);
4.打开组播准备接收设备发出的配网成功消息,收到消息即视为连接成功。(注:设备连接成功后会多次发出组播消息) (The multicast success message sent by the multicast preparation device is opened. If the message is received, the connection is considered successful. (Note: Multicast messages will be sent multiple times after the device is successfully connected))
String groupHost = "224.0.1.2";
int port = 6008;
String recieveData() {
String message = "";
try {
byte[] data = new byte[512];
DatagramPacket packet=new DatagramPacket(data, data.length,inetAddress,port);
multicastSocket.receive(packet);
message = new String(packet.getData(),0,packet.getLength());
} catch (Exception e1) {
System.out.println("Error: " + e1);
message = "Error: " + e1;
}
return message;
}