目前Android的WiFi自动连接的优先级规则如下:
1、priority值的范围设定为[0,1000000),如果超出此范围则会reset;
2、最近连接过的AP拥有最高priority,在自动连接中会首先尝试连接它;
3、未连接过但是扫描到的AP,按其信号值强弱排序,越强的显示靠前,但是,还得综合
AP的安全因素,基本情况是:WPA/WPA2 > WEP > signal level high > signal level low > noise low > noise
high
4、如果是预置的AP,可能会人为设定其最高的priority;
看一下源码,代码路径:frameworks/base/wifi/java/android/net/wifi/
WifiConfigStore.java
boolean selectNetwork(int netId) {
if (VDBG) localLog("selectNetwork", netId);
if (netId == INVALID_NETWORK_ID) return false;
// Reset the priority of each network at start or if it goes too high.
if (mLastPriority == -1 || mLastPriority > 1000000) {
Xlog.d(TAG, "Need to reset the priority, mLastPriority:" + mLastPriority);
for(WifiConfiguration config : mConfiguredNetworks.values()) {
if (workId != INVALID_NETWORK_ID) {
config.priority = 0;
addOrUpdateNetworkNative(config);
}
}
mLastPriority = 0;
}
// Set to the highest priority and save the configuration.
WifiConfiguration config = new WifiConfiguration();
workId = netId;
config.priority = ++mLastPriority;
addOrUpdateNetworkNative(config);
mWifiNative.saveConfig();
/* Enable the given network while disabling all other networks */
enableNetworkWithoutBroadcast(netId, true);
/* Avoid saving the config & sending a broadcast to prevent settings
* from displaying a disabled list of networks */
return true;
}
有时候,我们会忘记已连接WiFi的密码,应用市场也有相关的应用可以帮我们读取。其实如有有Root权限,用RE文件管理器(Root Explorer)就可以查看了。文件路径:
/data/misc/wifi/sockets/wpa_supplicant.conf
每一个network包裹起来的就是一个连接过的WiFi热点,其中ssid是名字,psk就是密码了,也可以看到其他信息,包括加密类型key_mgmt和优先级priority,是否自动连接autojoin等,如下图:
转载请注明出处:周木水的CSDN博客/zhoumushui
我的GitHub:周木水的GitHub/zhoumushui
原文:/zhoumushui/article/details/41276657