Android - How to wake up wifi? - android

I have an app that sends data every 5 min, i saw that wifi after some time goes sleep and stop working.
I found one solution that is use a WakeLock SCREEN_DIM_WAKE_LOCK.
The thing is: i can't have my screen waking (even dimmed) in this process. PARTIAL_WAKE_LOCK doesn't work in this case (to wake up wifi).
There is another solution for this problem?
UPDATE:
I'm using this topic method to lock wifi, and isn't work either:
Wifi sleeps, even with Lock
There is something wrong?

Have you tried using WifiManager.WifiLock? Creating and holding a wifiLock is the way your suppose to keep your radio awake enough to send.
Also check: Just prior to sending your message, print out what ConnectivityManager thinks about your TYPE_WIFI connection. Does the ConnectivityManager think all is well with your wifi?

Related

Keep Android awake for incoming network connections?

I'm writing an HTTP server for Android devices, implemented via NanoHTTPD.
A goal of mine is to have the device allow incoming connections even with the screen off.
I started small, with a persistent notification, thinking that would keep my app in memory and running in the background. After locking the device, I could keep navigating the webpages it serves up as long as I don't leave it alone for about a minute. Once I do, it totally stops responding.
I escalated my attempt by including a CPU partial wakelock, which made no difference. I then added a full WifiLock to keep the radio on, and finally, in desperation, a MulticastLock (I thought maybe it'd keep the radio listening for connections). Still, after not making any connections for about a minute, the device stops responding, even with all these locks.
Is there anything specific I can do to keep the device listening for incoming connections? It seems like hitting the device with periodic requests keeps it awake... can I somehow emulate this behavior programmatically? I cannot think of a way.
Thanks!
EDIT: for the purpose of this question, battery drain can be disregarded.
EDIT: NanoHTTPD is being run as a service, as well.
You need the following:
Foreground service
Wifi lock
CPU lock
Incomplete snippets (eg missing the services / relinquishing the locks):
// Keep the CPU awake.
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Httpd");
wakeLock.acquire();
// Keep the wifi awake.
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Httpd");
wifiLock.acquire();

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.

what does it mean "Wifi Watchdog Service (android.server.ServerThread) does not require the watchdog"?

I have implemented one application which talks over TCP/IP to the server via Wi-Fi, it has to talk to server even device goes to sleep mode so for that I implemented Partial wake lock so even though device goes to sleep mode CPU will be turned on so Wi-Fi also be turned on.
Problem: If I keep the device for longer duration around 7-8 hrs idle with my application running , it seems like device goes to deep sleep mode and when i checked the logcat i found the message : "Wifi Watchdog Service (android.server.ServerThread) for wifi does not require the watchdog".
Not able to undertsand why this message display for the router?
Can anyone put some good inputs regarding this?
Regards,
Piks

how to avoid deep sleep mode in android phone programmatically?

I have develop one android application which will talk to server 24*7 over WiFi but when phone goes to sleep mode it stops talking to server means socket is getting closed so for resolving this I added code to acquire partial wake lock in service onCreate() and release it on OnDestroy() method o service so even though phone goes to sleep mode my application can talk to the server.
Problem is: If you keep the device idle for longer period (more than 8 hours ), it stops communicating with the server and WiFi turned off. I heard about deep sleep mode of device, in this case it will shutdown CPU,WiFi etc.. so how to restrict it for getting CPU and Wifi turned off?
Please help me with some sample example.
Regards,
Piks
I assume you have tried full wake lock as well?

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