1. 程式人生 > >[RK3288][Android6.0] 主動從WLAN網路切換到移動資料網路

[RK3288][Android6.0] 主動從WLAN網路切換到移動資料網路

Platform: Rockchip
OS: Android 6.0
Kernel: 3.10.92

開發者選項有個欄位是“Aggressive WiFi to Cellular handover”,
這裡寫圖片描述
這個選項的功能是“當WiFi訊號較弱時,將主動從WiFi切換到移動資料網路”,根據之前對網路評分機制的瞭解,當WiFi分數較低的時候,那麼就會被“踢出局”由移動資料網路接替工作,那麼此選項是否也是通過影響評分值的方式呢?

“Aggressive WiFi to Cellular handover”對應的string name在

packages/apps/Settings/res/values/strings.xml:3643:
<string name="wifi_aggressive_handover">Aggressive Wi\u2011Fi to Cellular handover</string>

程式碼裡查詢wifi_aggressive_handover
DevelopmentSettings.java
private static final String WIFI_AGGRESSIVE_HANDOVER_KEY = "wifi_aggressive_handover";
mWifiAggressiveHandover = findAndInitSwitchPref(WIFI_AGGRESSIVE_HANDOVER_KEY);
當點選設定裡的switch時,觸發click函式

public boolean onPreferenceTreeClick()
{
    } else
if (preference == mWifiAggressiveHandover) { writeWifiAggressiveHandoverOptions(); } }

writeWifiAggressiveHandoverOptions()

private void writeWifiAggressiveHandoverOptions() {
    mWifiManager.enableAggressiveHandover(mWifiAggressiveHandover.isChecked() ? 1 : 0);
}

最終呼叫WiFiService

public
void enableAggressiveHandover(int enabled) { enforceAccessPermission(); mWifiStateMachine.enableAggressiveHandover(enabled); } void enableAggressiveHandover(int enabled) { mAggressiveHandover = enabled; }

mAggressiveHandover被呼叫的地方:

private void calculateWifiScore(WifiLinkLayerStats stats) {
    //rssi受mAggressiveHandover的值影響
    int rssi = mWifiInfo.getRssi() - 6 * mAggressiveHandover
            + (homeNetworkBoost ? WifiConfiguration.HOME_NETWORK_RSSI_BOOST : 0);
    sb.append(String.format(" rssi=%d ag=%d", rssi, mAggressiveHandover));

    boolean isBadRSSI = (is24GHz && rssi < mWifiConfigStore.thresholdBadRssi24.get())
            || (!is24GHz && rssi < mWifiConfigStore.thresholdBadRssi5.get());
    boolean isLowRSSI = (is24GHz && rssi < mWifiConfigStore.thresholdLowRssi24.get())
            || (!is24GHz && mWifiInfo.getRssi() < mWifiConfigStore.thresholdLowRssi5.get());
    boolean isHighRSSI = (is24GHz && rssi >= mWifiConfigStore.thresholdGoodRssi24.get())
            || (!is24GHz && mWifiInfo.getRssi() >= mWifiConfigStore.thresholdGoodRssi5.get());
}

可以看到當此選項開啟之後,rssi的值會變小,進而影響到到score的值,印證了一開始的猜測

不過這種做法讓人費解,相當於強制在程式碼中減少實際RSSI的值,然後讓WiFi強制切換到行動網路,不太理解Google的做法