简体   繁体   中英

Android: Wi-Fi strength is not changing when phone Sleeps

After doing some testings, I noticed that when the phone goes to sleep mode or lock mode, the Wi-Fi signal strength (RSSI) and the Wi-Fi link speed do not change anymore (I'm running aa service and recording strength and linkspeed in the background) On the other hand, even when the phone goes to sleep mode, GPS Location does change.

Is there any explanation to why does this happen for Wi-Fi strength? If so, is there a work-around for it?

Here is the function I use to get Wi-Fi information:

private int getStrengthFromWifi() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo Info = cm.getActiveNetworkInfo();
    if (Info == null || !Info.isConnectedOrConnecting()) {
        Log.i("MyAppLog - no data (Wifi or Cellular)",
                "No data (Wifi or Cellular)");
        return 0;
    } else {
        int netType = Info.getType();

        if (netType == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE);
            int linkSpeed = wifiManager.getConnectionInfo().getLinkSpeed();
            int wifiStrength = wifiManager.getConnectionInfo().getRssi();
            Log.i("MyAppLog - Link Speed + Wifi Strength",
                    "Wifi Connection : linkSpeed " + linkSpeed
                            + " and Wifi Strength " + wifiStrength);

            return wifiStrength;
        }
    }
    return 0;
}

You need to acquire a WIFI_MODE_FULL_HIGH_PERF WifiLock from the system in order to continue to receive responses to wifi events when the system goes into a low power state.

Please review the Android SDK documentation for wifi locks: https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html

And be sure to check out the answers to this stack overflow question for tweaking settings to ensure the wifi does not sleep: How do I keep Wifi from disconnecting when phone is asleep?

XDA has some useful information for you here: http://forum.xda-developers.com/showthread.php?t=1625705

The explanation is that when the device goes to sleep it tries to conserve power by turning off non-essential features. You can probably fetch a WakeLock to keep tracking signal strength and speed, but you'll drain the battery... so use this sparingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM