Keep Android awake for incoming network connections? - android

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();

Related

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

Android - How to wake up wifi?

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?

Android Bluetooth & WakeLock relationship

I'm working on an Android application that runs in the background and enables support for a Bluetooth accessory. I would like to be constantly listening in the background for the Bluetooth device to try and open a socket to the phone. My question is whether it is possible to achieve this without constantly keeping a partial wakelock, since that would obviously have severe battery consequences. So what I'm wondering is what effect Bluetooth has on the phone falling asleep. Does the phone stay awake when there is an open Bluetooth socket? Does the Bluetooth chip wake up Android automatically if a device tries to connect? I've attempted to do some testing to answer these questions, but it's difficult to isolate what is happening with wake locks; in addition, I don't know if I can rely on the behavior I observe or if it subject to change on other devices.
Yes, if your application is running and a thread is in serverSocket.accept() method, incoming connection wake up phone, so there is no need to use wakelocks. However, make sure that your service is in foreground and is not killed by system.
If you are developing it for devices target to marshmallow based or above, there is DOZE mode to treat such conditions. You then need not to worry about these thing. It can handle the WAKE_LOCK with appropriate mechanism.
The phone does not stay awake if there is an open Bluetooth socket, and neither does the Bluetooth chip wake up Android if a device tries to connect. Usually there is a background thread running to accept connections on the open port and as soon as a device tries to connect, it is this thread which reads the connection, gets some sort of authentication from the incoming device(I am assuming that there is a security protocol in place to accept any new incoming connections) and once the incoming connection is authenticated, an independent thread is created/notified to handle subsequent information exchange with this thread.
So the background process would consume some power and battery drain and it is also responsible for keeping Android partially awake(partially as its a background process and you can always control how frequently it checks for incoming connections). Usually this background process is not run always, its run only when Bluetooth is turned on in Android. So you can also create a thread which should run only when Bluetooth is switched on in Android, else it should sleep.

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