How to turn WIFI radio on? - android

According to this the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released, however holding a WifiLock adversely affects battery life. So is there any way to turn the wifi radio back on when it was turned off by the system?

Are you looking for a way to turn WiFi on every time it turns off? Or a way to turn it on when you need it? Either way, get a WiFi lock when you need WiFi, and release the lock when you don't need WiFi.
The reason that a WiFi lock drains the battery is because it may be keeping the WiFi on when it's not actually necessary. But if your goal is to keep the WiFi on, then avoiding a WiFi lock won't help the battery situation.

Related

Is there any way to keep WIFI always ON, ignoring even the Power mode set by the user?

I am making an android app which requires WIFI to be always ON even if phone gets locked. Also, power mode selected by user shouldn't make WIFI to turn OFF. For example, if user selects "Power Saving" mode than OS will automatically turn OFF the WIFI which I want to ignore & just keep it ON forever?
Is it achievable?
The only thing that you can do is use WifiLock. This keeps the WiFi radio on when it was already on. However:
It does not override manual user control (e.g., airplane mode)
AFAIK it will not change full Doze behavior, where network access is suspended (though the user could add your app to the battery optimization whitelist)
Some device manufacturers have added more aggressive power management features, and it is possible that WifiLock is ignored for them
Users may get irritated if they feel that you are using an excessive amount of power, and users can express their irritation in a variety of ways, few of which are pleasant

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.

Check for wifi networks signal in sleep mode

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.

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