[RK3288][Android6.0] WiFi之Autojoin对无线网络的选择机制

来源:互联网 发布:南方数据粘贴 编辑:程序博客网 时间:2024/06/10 02:11

Platform: Rockchip
OS: Android 6.0
Kernel: 3.10.92

当有两个或者两个以上的已经保存的无线网络可以连接时,系统通过attemptAutoJoin()来选择一个最优网络。
假设只有两个保存的无线网络,配置文件如下:
root@rk3288:/ # cat /data/misc/wifi/wpa_supplicant.conf

network={    ssid="RD-TA100-2.4G"    psk="Gerrit2All"    key_mgmt=WPA-PSK    priority=2}network={    ssid="Kris"    psk="20122008"    key_mgmt=WPA-PSK    priority=3}

来看attemptAutoJoin():

boolean attemptAutoJoin(){        //获取上一次选择的网络配置,我上次选的是ap: "Kris"        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();        if (lastSelectedConfiguration != null) {            age = 14000;        }        //获取最近配置过的网络,这里会获取到两个,也就是配置文件中的那两个        List<WifiConfiguration> list =                mWifiConfigStore.getRecentConfiguredNetworks(age, false);        for (WifiConfiguration config : list) {            //candidate保存当前网络配置            if (candidate == null) {                candidate = config;            } else {                //循环第二次时就会比较两个网络配置                int order = compareWifiConfigurations(candidate, config);                if (order > 0) {                    // Ascending : candidate < config                    //经过一番计算后,发现order>0,那么老的candidate网络配置就会被新的代替,                    //如果有多余两个保存的无线网络,那么依次计算并且符合条件后替换                    candidate = config;                }            }        }        if (mWifiStateMachine.shouldSwitchNetwork(networkDelta)) {      // !!! JNo: Here!                //发送自动连接event给WifiStateMachine                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT,                        candidate.networkId, networkSwitchType, candidate);                found = true;        }        return found;}

compareWifiConfigurations():

int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) {    int order = 0;    boolean linked = false;    // ephemeral的解释是    // Indicate that a WifiConfiguration is temporary and should not be saved    //nor considered by AutoJoin.    //正常连接时WifiStateMachine在connect的时候设置为false    //当是未信任网络时会设置为true    if (a.ephemeral && b.ephemeral == false) {        return 1; // b is of higher priority - ascending    }    if (b.ephemeral && a.ephemeral == false) {        return -1; // a is of higher priority - descending    }    // Apply RSSI, in the range [-5, +5]    // after band adjustment, +n difference roughly corresponds to +10xn dBm    //通过RSSI也就是信号强度计算得到一个order    order = order + compareWifiConfigurationsRSSI(a, b, mCurrentConfigurationKey);    // If the configurations are not linked, compare by user's choice, only a    // very high RSSI difference can then override the choice    //如果用户上次有选择过某个网络,那么又要根据选择的choice来重新计算最终的order    if (!linked) {        int choice;        choice = getConnectChoice(a, b, false);        if (choice > 0) {            // a is of higher priority - descending            order = order - choice;            if (a.visibility != null) {                a.visibility.lastChoiceBoost = choice;                a.visibility.lastChoiceConfig = b.configKey();            }        }        choice = getConnectChoice(b, a, false);        if (choice > 0) {            // a is of lower priority - ascending            order = order + choice;            if (b.visibility != null) {                b.visibility.lastChoiceBoost = choice;                b.visibility.lastChoiceConfig = a.configKey();            }        }    }    String sorder = " == ";    if (order > 0) {        sorder = " < ";    } else if (order < 0) {        sorder = " > ";    }    return order;}

调试时可以把debug log打开,对应的log如下:

07-06 15:41:00.357   557   608 D WifiAutoJoinController : attemptAutoJoin() status=wpa_state=SCANNING07-06 15:41:00.357   557   608 D WifiAutoJoinController : p2p_device_address=b2:f1:ec:49:50:cb07-06 15:41:00.357   557   608 D WifiAutoJoinController : address=b0:f1:ec:49:50:cb07-06 15:41:00.357   557   608 D WifiAutoJoinController : uuid=544ae0ce-fa0a-58d2-af21-81be288cb44c07-06 15:41:00.358   557   608 D WifiAutoJoinController : attemptAutoJoin() num recent config 2 ---> suppNetId=-107-06 15:41:00.358   557   608 D WifiAutoJoinController : attemptAutoJoin good candidate seen, bumped hard -> status="Kris"WPA_PSK status=007-06 15:41:00.358   557   608 D WifiAutoJoinController : attemptAutoJoin trying id=1 "Kris"WPA_PSK status=007-06 15:41:00.359   557   608 D WifiAutoJoinController : attemptAutoJoin good candidate seen, bumped hard -> status="RD-TA100-2.4G"WPA_PSK status=007-06 15:41:00.359   557   608 D WifiAutoJoinController : attemptAutoJoin trying id=0 "RD-TA100-2.4G"WPA_PSK status=0 current candidate "Kris"WPA_PSK07-06 15:41:00.359   557   608 D WifiAutoJoinController : attemptAutoJoin will compare candidate  "Kris"WPA_PSK with "RD-TA100-2.4G"WPA_PSK07-06 15:41:00.359   557   608 D WifiAutoJoinController :     compareWifiConfigurationsRSSI: "Kris"WPA_PSK rssi=-55,-127 boost=0 "RD-TA100-2.4G"WPA_PSK rssi=-47,-127 boost=007-06 15:41:00.359   557   608 D WifiAutoJoinController :         "Kris"WPA_PSK is5=false score=-55 "RD-TA100-2.4G"WPA_PSK is5=false score=-4707-06 15:41:00.359   557   608 D WifiAutoJoinController :     compareWifiConfigurationsRSSI "Kris"WPA_PSK rssi=(-55,-127) num=(1,0) < "RD-TA100-2.4G"WPA_PSK rssi=(-47,-127) num=(1,0) -> 807-06 15:41:00.360   557   608 D WifiAutoJoinController :     compareWifiConfigurations prefers "Kris"WPA_PSK over "RD-TA100-2.4G"WPA_PSK due to user choice of 60 order -> -5207-06 15:41:00.360   557   608 D WifiAutoJoinController : compareWifiConfigurations: "Kris"WPA_PSK > "RD-TA100-2.4G"WPA_PSK order -5207-06 15:41:00.360   557   608 D WifiAutoJoinController : attemptAutoJoin compareWifiConfigurations returned -5207-06 15:41:00.360   557   608 D WifiAutoJoinController : attemptAutoJoin networkSwitching candidate "Kris"WPA_PSK linked=false : delta=1000 07-06 15:41:00.361   557   608 D WifiStateMachine: shouldSwitchNetwork  txSuccessRate=0.00 rxSuccessRate=0.00 delta 1000 -> 100007-06 15:41:00.361   557   608 D WifiAutoJoinController : AutoJoin auto connect with netId 1 to "Kris"WPA_PSK07-06 15:41:00.361   557   608 D WifiAutoJoinController : attemptRoam: "Kris"WPA_PSK Found ee:29:f5:be:c5:6b rssi=-55 freq=243707-06 15:41:00.361   557   608 D WifiAutoJoinController : Done attemptAutoJoin status=3

总的来说,评分机制有三种:
1. ephemeral值,即是否是未信任网络
2. RSSI值
3. 用户上次选择,优先级比RSSI高,除非RSSI非常大的情况

参考:
WifiAutoJoinController.java源码解析

阅读全文
2 0