Is using WakeLock overkill while using WifiLock on Android?
Asked Answered
B

1

5

My audio streaming app is working correctly with only WifiLock.

But some devices report that when they turn off the screen, connection is cut and audio streaming is topped. (They say this does not happen on 3G)

So, I thought maybe I can also use a Partial WakeLock. I am not sure if this fixes the issue but I think it is an option. At the same time, adding a WakeLock while a basic WifiLock is working may deplete the battery faster and may not fix the issue.

What do you think about adding a WakeLock too for this kind of issue and app?

private static WifiLock wifiLock = ((WifiManager) appInstance().getSystemService(Context.WIFI_SERVICE))
        .createWifiLock((android.os.Build.VERSION.SDK_INT>=12?WifiManager.WIFI_MODE_FULL_HIGH_PERF:WifiManager.WIFI_MODE_FULL), "myappwifilock");

The newly added line:

private static WakeLock wakeLock= ((PowerManager) appInstance().getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myappwakelock");

(ofcourse I acquire and release these locks on the creation of the service and on the destruction of it.)

Brahmana answered 18/11, 2013 at 9:32 Comment(6)
Related: #14609158Healing
They are different things. Partial WakeLock keeps the CPU running, while WiFiLock keeps the WiFi radio on. WakeLock w/o WifiLock also turns on Wi-Fi, but it needs a while (30s to 1min) to connect to the network again, so it's likely that first connections will fail.Healing
So I "must" add the code line that I've noted in the question for WakeLock too?Brahmana
@MisterSmith: "WakeLock w/o WifiLock also turns on Wi-Fi" -- ?! No. Are you sure ?Cleave
@Cleave I've only tested it on a couple models OS 4.x, but this seems to be the case both with partial and screen locks. Problem is, as I said, Wi-Fi isn't fully connected until a little while later (reconnection time).Healing
@MisterSmith: Don't rely on this - here is what I do to wake it up - it certainly does not take 1 -1,5 minutes to reconnect - (around 4 secs) - but I actively call reconnect etc : https://mcmap.net/q/408856/-android-reconnect-to-wi-fi-after-entering-coverage-area-while-screen-turned-offCleave
C
3

Use them both. The behavior I am sure varies from phone to phone. You may wish to search for the devices the reports are about + "wifi"or "wifi driver". Are you sure your audio streaming app is working correctly with only WifiLock ? This sounds very strange - the CPU will go to sleep and the service will stop - see Service, WakeLock. Something else is keeping the phone awake. So you need a wake lock

If you only use wake lock on the other hand the wifi will turn off maybe - I am not sure for you are using it - but better safe than sorry. If it does turn off waking the phone up won't wake it up - for this I am sure. Using the wifi lock has no impact on the battery - using the wifi radio has, and this you are doing anyway.

So both - and be sure your service acquires them - have a look at WakefulIntentService

Cleave answered 19/11, 2013 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.