目前Android的WiFi自动连接的优先级规则如下:
1、priority值的范围设定为[0,1000000),如果超出此范围则会reset;
2、最近连接过的AP拥有最高priority,在自动连接中会首先尝试连接它;4、如果是预置的AP,可能会人为设定其最高的priority;
看一下源码,代码路径:frameworks/base/wifi/java/android/net/wifi/
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 (config.networkId != INVALID_NETWORK_ID) { config.priority = 0; addOrUpdateNetworkNative(config); } } mLastPriority = 0; } // Set to the highest priority and save the configuration. WifiConfiguration config = new WifiConfiguration(); config.networkId = 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; }
/data/misc/wifi/sockets/wpa_supplicant.conf
每一个network包裹起来的就是一个连接过的WiFi热点,其中ssid是名字,psk就是密码了,也可以看到其他信息,包括加密类型key_mgmt和优先级priority,是否自动连接autojoin等,如下图:
转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui
我的GitHub:周木水的GitHub https://github.com/zhoumushui
Android自动连接WiFi优先级规则,以及查看已连接WiFi的密码
原文地址:http://blog.csdn.net/zhoumushui/article/details/41276657