How to keep CPU from 'sleeping' when screen is turned off in Android?
Asked Answered
L

2

15

I have an application in which I am sending network data over WiFI. Everything is fine until I turn the display off or the device goes to 'sleep'. I'm already locking the WiFi however, it seems to be the case that the CPU speed ramps down when in sleep which causes my streaming to not behave properly (i.e. packets don't flow as fast as I would like as they do when the device is not sleeping).

I know that I possibly can/possibly should address this at the protocol level however, that might possibly not be possible as well...

Is there any way to "prevent the CPU from going to 'sleep' when the screen is off"? If so, how? If not, any advice on how to keep the speed of my WiFi stream consistent whether the device is in sleep mode or not?

Lindyline answered 1/10, 2011 at 1:5 Comment(0)
L
11

Okay, so, after much more research and experimenting, it seems that the real issue is the fact that, at least on some phones, their WiFi goes into a 'partial sleep' mode EVEN IF you've taken the WiFi lock. It seems that this is what the 'WIFI_MODE_FULL_HIGH_PERF' flag was invented for when taking the WiFi lock... unfortunately, this flag is only available on some devices/Android versions (I have no clue as to which but, it wasn't available to me). So, therefore, it isn't a fix for all devices.

The only "solution" (which is actually a kludge) seems to be to 'detect when the screen is turned off and then, set an alarm that turns the screen back on immediately thereafter'. The links that helped a little bit with this are:

How to keep a task alive after phone sleeps?

and

http://android.modaco.com/topic/330272-screen-off-wifi-off/

I hope that this helps people who are experiencing WiFi disruption when the phone goes to sleep/screen is turned off (and the phone is unplugged/disconnected [e.g. you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]).

Lindyline answered 11/10, 2011 at 16:30 Comment(3)
+1 for actually posting your solution - so it wasn't an issue with the CPU lock but with the (phone) implementation of the Wifi lock ?Beauty
Apparently it was left to the implementation as to how to adhere to the wifi lock on the older Android versions and, yeah, on my old HTC Evo 4G (it was running Gingerbread/Android 2.3), taking the WiFi lock would result in the WiFi staying on but it's performance being restricted so we added an option specifically for devices such as that one to "keep the screen on" (which would keep the screen on even if they try to turn it off [e.g. my 'kludge' solution above]) and that would allow for the WiFi to stay in it's normal, high performance mode.Lindyline
@fatfreddyscat, I realize this was nearly a decade ago, but did the alarm workaround ultimately work for you? Was the alarm visible on the screen or was it behind-the-scene?Temporal
A
14

Grab a PARTIAL_WAKE_LOCK from the PowerManager. You'll also need to add the WAKE_LOCK permission to your manifest.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag"); 
wl.acquire();
//do what you need to do
wl.release(); 
Acceptance answered 1/10, 2011 at 1:13 Comment(7)
Thanks! I didn't realize that the 'PARTIAL_WAKE_LOCK' was related to cpu. (not enough research on my part, sorry)Lindyline
HOWEVER, my wifi performance is still degraded upon turning the screen off. I am (and already was prior to submitting this question) taking the WiFi wake lock and, now, also taking the CPU wake lock but, still getting degradation. I suppose a follow up would be: "is there any way to 'check the cpu speed at run time'... well, I found a method at: #3021554Lindyline
using the method posted by 'zed_0xff' to print the contents of the files posted by 'Ellis' but unfortunately, it just gives the string representation of the byte array or something (e.g. unreadable). Do you know of a way to "correctly show the CPU speed" in a "human readable form"? That way I could test whether the CPU is in fact staying the same speed or not. Otherwise, I'm not sure of any other possible solutions to my issue?Lindyline
also, by the way, I'm only seeing this behavior (wifi performance degradation) when my phone is not plugged in (e.g. if I'm running the app through adb with the phone plugged to my computer, I don't see the issue; it's only when the phone isn't plugged in).Lindyline
so I verified that the contents of the files "/proc/cpuinfo" "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" are the same incl. BogoMIPS whether the screen is on or off. That includes whether I take the PARTIAL_WAKE_LOCK or not. So, is there some way to verify that the lock is actually keeping the cpu from slowing down? My method of dumping the contents of those files doesn't seem to work to verify this (if the partial wake lock is, in fact, keeping the CPU from slowing down)Lindyline
@fatfreddyscat: If a PARTIAL_WAKE_LOCK doesn't do the trick, then I'm afraid that I don't have any other ideas.Acceptance
Okay, thanks, I absolutely must get this figured out so, when I do, I will comment here (and I will post the solution). Thanks again for your help!Lindyline
L
11

Okay, so, after much more research and experimenting, it seems that the real issue is the fact that, at least on some phones, their WiFi goes into a 'partial sleep' mode EVEN IF you've taken the WiFi lock. It seems that this is what the 'WIFI_MODE_FULL_HIGH_PERF' flag was invented for when taking the WiFi lock... unfortunately, this flag is only available on some devices/Android versions (I have no clue as to which but, it wasn't available to me). So, therefore, it isn't a fix for all devices.

The only "solution" (which is actually a kludge) seems to be to 'detect when the screen is turned off and then, set an alarm that turns the screen back on immediately thereafter'. The links that helped a little bit with this are:

How to keep a task alive after phone sleeps?

and

http://android.modaco.com/topic/330272-screen-off-wifi-off/

I hope that this helps people who are experiencing WiFi disruption when the phone goes to sleep/screen is turned off (and the phone is unplugged/disconnected [e.g. you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]).

Lindyline answered 11/10, 2011 at 16:30 Comment(3)
+1 for actually posting your solution - so it wasn't an issue with the CPU lock but with the (phone) implementation of the Wifi lock ?Beauty
Apparently it was left to the implementation as to how to adhere to the wifi lock on the older Android versions and, yeah, on my old HTC Evo 4G (it was running Gingerbread/Android 2.3), taking the WiFi lock would result in the WiFi staying on but it's performance being restricted so we added an option specifically for devices such as that one to "keep the screen on" (which would keep the screen on even if they try to turn it off [e.g. my 'kludge' solution above]) and that would allow for the WiFi to stay in it's normal, high performance mode.Lindyline
@fatfreddyscat, I realize this was nearly a decade ago, but did the alarm workaround ultimately work for you? Was the alarm visible on the screen or was it behind-the-scene?Temporal

© 2022 - 2024 — McMap. All rights reserved.