AP模式配网 (AP mode distribution network)
1.设备播完 “请连接的语音” 后会打开一个WIFI热点,热点名称为 camera_+UID 密码为 “12345678”,APP连接时需要获取 WIFI 权限,接着获取 WIFI 列表 (After the device finishes “Please connect voice”, it will open a WIFI hotspot. The hotspot name is camera_+UID and the password is “12345678”. When the APP is connected, you need to obtain WIFI permission, and then get the WIFI list.)。
2.确保手机连上设备的WIFI热点 (Make sure your phone is connected to the device's WIFI hotspot)
3.建立与设备的TCP连接 (Establish a TCP connection to the device)
private int retryTimes=5;
private void connectCamera(){
new Thread(() -> {
KLog.e("尝试连接 (Try to connect)*************************");
StringBuilder builder = new StringBuilder();
TimeZone aDefault = TimeZone.getDefault();
int rawOffset = aDefault.getRawOffset();
rawOffset = rawOffset / (1000 * 60);
String wifiString = new StringBuilder()
.append("S=").append(ssid).append(",")
.append("P=").append(pwd).append(",")
.append("T=").append(1).append(",")
.append("Z=").append(rawOffset).append(",")
.append("A=").append("账号").append(",")
.append("U=").append("公司名称或者app名称")
.toString();
try {
connectByTcp(wifiString);
} catch (Exception e) {
e.printStackTrace();
KLog.e(e.getMessage()+"*"+retryTimes);
if (retryTimes>0){
retryTimes--;
connectCamera();
}
}
}).start();
}
4.接收TCP回调判断是否连接成功 (Receive TCP callback to determine whether the connection is successful)
private void connectByTcp(String wifiString) throws IOException {
Socket socket = null;
socket = new Socket("192.168.201.1",10000);
socket.setSoTimeout(5000);
Socket finalSocket = socket;
new Thread(() -> {
try {
finalSocket.getOutputStream().write(wifiString.getBytes());
finalSocket.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
String s = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
KLog.e(s);
SystemClock.sleep(5000);
}