Skip to content Skip to sidebar Skip to footer

Wifilock And Wakelock Not Working Correctly On Android

I am developing an app that needs to use both wifiLock and wakeLock so the audio streaming when the screen is off does not get disturbed. I have tried my app on Android 2.3 and wak

Solution 1:

I only just stumbled across this question - probably a couple of years too late for the OP, but just in case it helps someone else, you probably need to use WIFI_MODE_FULL_HIGH_PERF instead of WIFI_MODE_FULL, as that can cause streaming issues:

wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");

I'd probably look at streaming the audio in a Service instead of an Activity too, especially as you appear to be designing it with the idea of running in the background.

Solution 2:

It is so late to post the answer but maybe it helps someone. As i faced the same problem and got the solution as :

WifiManagerwm= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");
        wifiLock.acquire();
        PowerManagerpm= (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();

And release the locks in destroy method:

if (wakeLock != null) {
            if (wakeLock.isHeld()) {
                wakeLock.release();
            }
        }
        if (wifiLock != null) {
            if (wifiLock.isHeld()) {
                wifiLock.release();
            }
        }

What i think is missing in your case is you're not acquiring locks. See this link.

Solution 3:

You are acquiring the lock in an activity and when your activity goes in the stack the system can remove and destroy your activity and this maybe the cause of your problem. Try to acquire the lock in a service. But the combination of PARTIAL lock and WIFI does not work properly on some devices as well see this question: Android WifiLock not working?

Post a Comment for "Wifilock And Wakelock Not Working Correctly On Android"