I run a number of backgrounded applications on my Android phone, such as Nagios (server monitoring) CSipSimple (VoIP/SIP), OpenVPN (SSL-based VPN) and IMAP idle (push email).
Whilst this does impact battery life somewhat, I’ve got things reasonably well tuned so that the frequency of polling and keepalives is just long enough to prevent firewall timeouts, but long enough to avoid excessive waking of 3G & wifi hardware.
(For example, the default OpenVPN keepalive of 10 seconds is far more aggressive than what is actually needed, in reality, I was able to drop my phone back to one keepalive every 5 minutes – short enough to keep sessions active, but long enough that the transmitting hardware can sleep regularly whilst it isn’t needed).
However one problem I wasn’t able to fix, was the amount that the wifi disconnected – this would really screw up things, since services such as IMAP idle and SIP were trying to run over the VPN, but this would be broken by the VPN being dropped when wifi turned itself off.
I found the fix thanks to a friend who came around and told me about the hidden “Advanced” menu in the wifi network selection page:
When on the wifi network selection screen, you need to press the menu key (not sure what the option is for newer Android phones that don’t have the menu key any more?) and then a single “Advanced” menu item will appear.
Selecting this item will give you a couple extra options, including the important “Keep Wi-Fi on during sleep” option that stops the phone from dropping the wifi connection whenever you turn off the screen.
This resolved my issues with backgrounded services and I found it also made the phone generally perform better when doing any data-related services, since wifi didn’t have to renegotiate with the AP as frequently.
It’s not totally perfect, Android seems to sometimes have an argument with the AP and then drop the connection and waste a minute trying to reconnect, but it’s a lot better than it was. :-)
I’ve got the Galaxy S3 running Icecream Sandwich. It’s not at all hidden on my phone. The ‘Advanced’ button and all those settings are viewable by default.
Hmm interesting, I wonder if it’s the newer Android phones handling it different now that the menu button has been deprecated.
The screenshots above are from stock ICS 4.0.3, I’ve tested and 2.2.2 also has the same UI flaw – it’s possible the behavior has changed in newer versions though, or that some vendors (eg Samsung) have tweaked their version of Android slightly.
with openvpn, how do you handle disconnecting from a wap and using 3g. the vpn won’t realise that it’s down for 5 minutes?
The current generation of OpenVPN apps for Android are connection aware, changing between WiFi and 3G will trigger it to initiate a re-connection.
Also note that even without keepalives and connection awareness, if you’re sending traffic over the VPN and the connection drops and changes, it will realize it’s unable to send traffic and initiate a re-connection.
sorry to correct myself. with a keep alive of 5 minutes the soonest the client can detect it has been disconnected is 10 minutes. am I missing something?
Keepalive means it sends a packet every 5 minutes to keep the connection active – if the connection has dropped, it would cause a re-connection.
At the most, it would take 5 minutes to detect a loss of connectivity.