Check for wifi networks signal in sleep mode - android

My objective is to be able to scan changes in Wi-Fi networks (mainly to check what networks are available). Right know I'm doing it by registering a reciever:
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
where WifiReceiver is my BroadcastReceiver.
The problem is that it works only when the phone is on. As soon as it goes to sleep mode (either by pushing the power button or after some time), the receiver stops receiving. I already acquired a wake lock in onCreate of my main class and releasing it in onDestroy (it's a partial wake lock). Additionally I've tried this:
Settings.System.putInt(getContentResolver(),Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
to keep wifi from sleeping. Unfortunately that didn't help.
Is there any possibility to scan for network changes, even when phone is asleep? I want to be able to check what networks are in range (by SSID). Maybe I should use another action?
Appreciate your help.

Literally, no, the hardware isn't typically designed nor the low level drivers written to allow wifi to operate while the application CPU is in sleep mode - unlike the case with the mobile network interface which is intended to receive push events like phone calls, sms, and gmail "tickles".
So your choices are to either manage to successfully prevent the device from sleeping (good bye battery) or wake up periodically using an alarm, kick the wifi active, and poll the situation (still not good for battery life, but not as drastically so).

I'm afraid this sleep behaviour is managed by the system under (on Android 2.3.4) Settings > Wireless & Networks > Wi-Fi Settings > Press Menu > Advanced > Wi-Fi Sleep policy.
This could actually go against the users will, careful with that.
However I think there has to be a way to modify this param. programmatically, using this settings : Settings.System, and of course the corresponding permission in your manifest.

Related

Android opportunistic communication vs power management (what happens to service when phone goes to sleep)

I am writing an application for android that will use opportunistic communication. So when two devices are nearby they exchange data. For now it will happen when they are in the same wifi network (using mdns) or when they have paired bluetooth interfaces.
I was wondering from power manager point of view. Will my ongoing service be paused when phone enters sleep mode? If yes how do I prevent it?
Also any general tips how to make it consume as little power as possible? I was thinking to wake it up from time to time, but then there's a chance that it will miss a mdns query sent by other device which I want to avoid to maximize throughput.
And just to clarify - yes, I do want to use opportunistic communication and no I do not want to use any solution that involves connecting to the internet.
Any help appreciated.
Use Wakelock to indicate that your application needs to have the device stay on.
but it consumes considerable amount of power.
you can also use AlarmManager to start your app at that particular time when wifi is available

Different possibilites for keeping wifi radio turned on?

I have been reading as much as I can regarding the developer docs for the Android SDK about the following:
Wifi wakelock
WifiManager.WIFI_MODE_FULL_HIGH_PERF
Wifi sleep policy
They all make sense in their own context but I can't see the "big picture" so to speak.
I guess that both the wifi wakelock and the WIFI_MODE_FULL_HIGH_PERF setting overrides the user sleep policy. Is that right?
If I have the sleep policy on "Never", and I just make experimental APK:s for myself which is downloading stuff in the background via wifi, I guess I don't need the wifi wake lock nor the WIFI_MODE_FULL_HIGH_PERF?
The code I have seen using a wifi wake lock has also acquired a power wake lock. Is there any occasion where I only want to acquire the wifi lock but not the power lock?
If the device goes into a deep sleep and shuts down wi-fi radio, the wi-fi lock will wake it back up when the lock is acquired. Correct, this is regardless of your wi-fi policy.
I think you have it backwards- the policy question says "Keep Wi-Fi on during sleep" so if you want to avoid the need for wifi locks, you would choose the "Always" option value. I'd like to see a battery consumption analysis of Wi-Fi stay connected, versus sporadic 4G in deep sleep...
I have never needed or used a power wake lock. Just system and wifi, and everything has worked fine for me to perform the functions desired.

Keeping services awake without draining battery

I have an application that needs to continuously listen for incoming requests over wifi. A service that runs in the background does this job. However, this service falls asleep after a while when the screen turns off.
The solution from what I have searched is to use AlarmManager to keep it awake. But it is said that this will drain the battery of the device.
So, is there another way to do this?
For eg, what do apps like Whatsapp and Skype do? They don't seem to kill too much battery but they have continuously running services right?
Also, in case AlarmManager is the only way, it would be really kind if someone could share a tutorial or example for it.
The solution from what I have searched is to use AlarmManager to keep it awake
That will not help. Once the device falls asleep, your socket connection will be terminated. You would need to use a partial WakeLock plus a WifiLock to keep the device powered on continuously.
But it is said that this will drain the battery of the device.
The WakeLock and WifiLock will definitely drain the battery.
So, is there another way to do this?
Not if you need to use WiFi.
For eg, what do apps like Whatsapp and Skype do?
They do not use WiFi when the device wants to go to sleep. Once the WiFi radio powers down, they use mobile data, so no WifiLock is needed. For mobile data, incoming packets will wake up the device, so you only need a WakeLock while you are actually doing work, rather than constantly.
The best answer is to switch to use C2DM, though.
Actually its not your service which falls to sleep, its your WiFi unit on the device. Manufacturers like HTC (or perhaps all Android devices) have implemented this kind of behavior on their devices in which the WiFi unit goes standby after certain time period of screen-off. This helps the devices to save battery when its not being used.

android WIFI state when phone is locked

when android phone is in SLEEP State 0r Locked State What is the state of WIFI ?
is it active or not?
It's depend on application. By default in Sleep mode wifi state is inactive. Application need to take a wifi lock to keep it acive.
It depends on many factors, including those that others have metioned:
In your android settings, wireless settings, advanced settings- there is a wifi sleep policy which can somewhat answer your question.
However, an application (with the appropriate permission) can change this setting or temporarily change it to bypass the setting.
"Sleep" is pretty vague because some consider this when your display is turned off, however a device actually has different levels of sleep, and deep sleep. Deep sleep doesn't kick in as soon as the display is off, and often these rules are referring to deep sleep.
Applications can also obtain a wifi lock (with the appropriate permission) which will wake up the Wi-Fi radio even though the phone is in deep sleep.
Must be active, I've downloaded stuff from the Market place whilst my phone (HTC Desire) has gone to sleep, and the download has finished the next time I unlock (wake) it.

Android Froyo and Wifi

By default, Wifi sleep policy is "Sleep on screen idle".
With this policy, is it possible for a Background Service at a later time to wake up Wifi using some API?
Am trying the following, but does not work:
When my Background Service wakes up, it calls "ConnectivityManager.getActiveNetworkInfo()" to get active network.
Since, the wireless is off on idle, I tried waking it up using "WifiManager.startScan" on a previously used Wifi connection.
But still dont get Wifi connectivity.
Any ideas?
I preferably do not want to change my sleep policy to "Never".
Thanks
Hemant
There are no real simple solutions for this. To with a high probability ensure you have WIFI connectivity when the phone/screen goes to sleep the best way is to turn it off. Look here for a lot of details - http://wififixer.wordpress.com/
It is important to realize that in sleep mode the Wifi enters a low power mode. This will become tricky then to programmatically check as it might have connectivity to the Wifi but the Wifi connection is too weak or too slow to complete the HTTP request and hence it times out. This would force you to also check the speed of the Wifi connectivity as well as you will have an active network but a pretty lousy one.
Proper handling of the escaping when timeout occurs for the HTTP call you make makes it ok to use but ultimately the only way to have a background thread constantly running to get data is only doable when you have the Wifi mode to never sleep.
It is tricky and not the best way I know. :-( It is however the only path I have found which is reliable enough.

Categories

Resources