Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Android device hold a TCP connection to Internet without wake lock?

I want my application to be connected to server though the mobile connection, yet allowing the device to go into sleep mode. I expect it to wake up when IP packates arrives.

How can this be done? How to receive "interrupts" from the Internet without draining battery?

like image 282
Vi. Avatar asked Nov 23 '12 19:11

Vi.


3 Answers

When you are blocked on a read from a tcp stream the device can go into a deep sleep and when tcp traffic comes in it will briefly wakeup the device, as soon as a bit is read in you start a wakelock until you have received the whole transmission then release it.

Here is an example with web sockets, I've ran this app for over 12 hours in the background with no battery impact. https://github.com/schwiz/android-websocket-example

The client is here, the blocking read is in the start method. https://github.com/schwiz/android-websockets/blob/master/src/com/codebutler/android_websockets/HybiParser.java

like image 106
Nathan Schwermann Avatar answered Oct 07 '22 17:10

Nathan Schwermann


I've been using long living TCP connections on Android without a wake lock for some years now.

My experience is that when data arrives on a TCP connection and the device is in deep sleep, it will be woken up for a short period of time at least. Waking up the device could take up to ~2 minutes sometimes, but it's usually done within a few seconds.

Now that the device is awake, the receiving process has some time too process the data. Now either the process is able to do so before the device is put back into deep sleep, or the device will be put into deep sleep suspending also the process. The important thing here is that the data is not lost, it remains in the memory and the process is able to resume the work processing the data the next time the device leaves deep sleep. Of course, this means that if the sender awaits an answer to his data, it may take some time, until he gets it.

You could take a wake lock as soon as your network library notifies you that a new message was received. But if you done, then make sure to handle the lock properly, i.e. ensure that it is released at some point and in every code path. I personally never experienced the need for a wake lock, the Android device was always long enough awake to process the request. But your millage may vary.

like image 21
Flow Avatar answered Oct 07 '22 17:10

Flow


So this is very old but i ended up testing the behaviour @Flow described and just wanted to confirm that there seam to be arbitrary delays sometimes between the arrival of the data and the wakeup of the device.
I tested using a tcpClient implementation and an mqttimplementation. The idea was to see if there is an requirement of instantly getting the wakelock since this delay appeared in my mqtt implementation.
Test steup:

  • we have 2 services one running the tcpclient and one running the mqttclient in different apps
  • Both Services run on the same phone with the same permissions in the background.
  • The Server sends in both cases an "ping" message.
  • Our client implementation acquires a wakelock as soon as possible and reads the current Date.
    • for the tcpclient this is instantly
    • for the mqttclient the wakelock can only be acquired after the arriving data has been propagated through the networking libraries
  • we send back an response pong message including the read date
    • this send happens after wakelock release to see if this further delays the response time
  • the server logs incoming messages with the arrival and the read date

It appears that in both implementations there sometimes is an arbitrary delay to the call to our code. This makes it most likly that there is a delay to the wakeup of the device and not to the acquire of the wakelock.

  • this delay can be sometimes seen on all devices(tested on huaweip20light, HMD Global#Nokia 7.2, samsung#SM-N960F)
  • this delay seams more likly to happen on the HMD device higher api and victim of the stricter battery optimisations android established
like image 1
flopfl Avatar answered Oct 07 '22 16:10

flopfl