Commit 16bc5455 by pye52

修复wifi无法链接的问题

parent cbe693e4
package com.bgycc.smartcanteen.command; package com.bgycc.smartcanteen.command;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
...@@ -28,49 +30,59 @@ public class WifiConfigCommandWorker extends CommandWorker { ...@@ -28,49 +30,59 @@ public class WifiConfigCommandWorker extends CommandWorker {
if (data == null) { if (data == null) {
return success(); return success();
} }
try {
String ssid = data.getSsid();
String identity = data.getIdentity();
if (identity == null) {
identity = "";
}
String pwd = data.getPwd();
String type = data.getType();
WifiConfiguration currentConfiguration = NetworkUtils.getWifiConfiguration(ssid);
if (currentConfiguration != null) {
NetworkUtils.disconnect(currentConfiguration.networkId);
}
if (NetworkUtils.enable(ssid, identity, pwd, type)) {
NetworkUtils.reconnect();
}
if (!NetworkUtils.isWifiEnabled()) {
inProgress("正在启动Wifi");
if (!NetworkUtils.setEnable(true)) { if (!NetworkUtils.setEnable(true)) {
String failedMessage = "无法启动Wifi"; String failedMessage = "无法启动Wifi";
inProgress(failedMessage); inProgress(failedMessage);
LogUtils.e(TAG, failedMessage); LogUtils.e(TAG, failedMessage);
try { Thread.sleep(DEFAULT_DELAY);
Thread.sleep(DEFAULT_DELAY);
} catch (Exception ignored) {
}
return success(failedMessage); return success(failedMessage);
} }
}
// 断开当前在链接的wifi // 断开当前在链接的wifi
WifiInfo currentWifiInfo = NetworkUtils.getWifiInfo(); WifiInfo currentWifiInfo = NetworkUtils.getWifiInfo();
if (currentWifiInfo != null) { if (currentWifiInfo != null) {
NetworkUtils.disconnect(currentWifiInfo.getNetworkId()); NetworkUtils.disconnect(currentWifiInfo.getNetworkId());
LogUtils.d(TAG, "断开当前wifi: " + currentWifiInfo.toString()); LogUtils.d(TAG, "断开当前wifi: " + currentWifiInfo.toString());
} }
String ssid = data.getSsid();
String identity = data.getIdentity();
String pwd = data.getPwd();
String type = data.getType();
inProgress("正在配置Wifi"); inProgress("正在配置Wifi");
LogUtils.d(TAG, "开始配置wifi, ssid: " + ssid + ", identity: " + identity + ", pwd: " + pwd + ", type: " + type); WifiConfiguration targetConfiguration = NetworkUtils.createWifiConfiguration(ssid, identity, pwd, type);
try { NetworkUtils.disconnect(targetConfiguration.networkId);
NetworkUtils.connect(ssid, identity, pwd, type); LogUtils.d(TAG, "开始配置wifi, ssid: " + ssid + ", identity: " + identity + ", pwd: " + pwd + ", type: " + type);
NetworkUtils.enable(ssid, identity, pwd, type);
// 轮询检查wifi是否链接成功 // 轮询检查wifi是否链接成功
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
Thread.sleep(DEFAULT_DELAY);
WifiInfo info = NetworkUtils.getWifiInfo(); WifiInfo info = NetworkUtils.getWifiInfo();
if (info == null || info.getIpAddress() == 0) { if (info == null) {
continue;
}
if (info.getIpAddress() == 0) {
inProgress("当前链接wifi: " + info.getBSSID());
continue; continue;
} }
String message = "Wifi配置成功"; String message = "Wifi配置成功";
inProgress(message); inProgress(message);
LogUtils.d(TAG, message); LogUtils.d(TAG, message);
Thread.sleep(DEFAULT_DELAY);
return success(message); return success(message);
} }
Log.d(TAG, "轮询结束,仍然未能成功链接wifi: " + ssid);
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e); LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e);
return success(e.getMessage()); return success(e.getMessage());
......
...@@ -22,6 +22,7 @@ import com.blankj.utilcode.util.LogUtils; ...@@ -22,6 +22,7 @@ import com.blankj.utilcode.util.LogUtils;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG; import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
...@@ -234,33 +235,48 @@ public class NetworkUtils { ...@@ -234,33 +235,48 @@ public class NetworkUtils {
connectivityManager.requestNetwork(request, callback); connectivityManager.requestNetwork(request, callback);
} }
public static boolean connect(String ssid, String identity, String pwd, String type) { public static boolean enable(String ssid, String identity, String pwd, String type) {
return connect(createWifiConfiguration(ssid, identity, pwd, type)); return enable(createWifiConfiguration(ssid, identity, pwd, type));
} }
public static boolean connect(String ssid, String pwd, String type) { public static boolean enable(String ssid, String pwd, String type) {
return connect(createWifiConfiguration(ssid, null, pwd, type)); return enable(createWifiConfiguration(ssid, null, pwd, type));
} }
public static boolean connect(String ssid, String pwd) { public static boolean enable(String ssid, String pwd) {
return connect(createWifiConfiguration(ssid, null, pwd, null)); return enable(createWifiConfiguration(ssid, null, pwd, null));
}
public static boolean reconnect() {
return wifiManager.reconnect();
} }
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
public static boolean connect(WifiConfiguration config) { public static boolean enable(WifiConfiguration config) {
wifiManager.addNetwork(config); int netId = wifiManager.addNetwork(config);
if (!wifiManager.enableNetwork(config.networkId, true)) { return wifiManager.enableNetwork(netId, true);
return false; }
@SuppressLint("MissingPermission")
public static WifiConfiguration getWifiConfiguration(String ssid) {
try {
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals("\"" + ssid + "\"")) {
return existingConfig;
}
}
} catch(Exception e) {
e.printStackTrace();
} }
wifiManager.reconnect(); return null;
return false;
} }
public static boolean disconnect(int netId) { public static boolean disconnect(int netId) {
return wifiManager.removeNetwork(netId); return wifiManager.removeNetwork(netId);
} }
private static WifiConfiguration createWifiConfiguration(String ssid, String identity, String pwd, String type) { public static WifiConfiguration createWifiConfiguration(String ssid, String identity, String pwd, String type) {
if(ssid == null || ssid.isEmpty()) if(ssid == null || ssid.isEmpty())
return null; return null;
if (pwd != null && !pwd.isEmpty()) { if (pwd != null && !pwd.isEmpty()) {
...@@ -278,7 +294,7 @@ public class NetworkUtils { ...@@ -278,7 +294,7 @@ public class NetworkUtils {
config.allowedProtocols.clear(); config.allowedProtocols.clear();
config.SSID = "\"" + ssid + "\""; config.SSID = "\"" + ssid + "\"";
if (type == null) { if (type == null || type.isEmpty()) {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if ("wep".compareToIgnoreCase(type) == 0) { } else if ("wep".compareToIgnoreCase(type) == 0) {
config.hiddenSSID = true; config.hiddenSSID = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment