Tal Kanel
Tal Kanel

Reputation: 10785

determine 3G/WIFI radio state machine current state in Android device

my application receives constantly location updates and activity recognition updates, and sending in one batch once in a while all the collected activity and location records to the server.

I know that every time I perform network operations - the radio state of the WIFI or mobile (depends to what is the current active connection) is going to active state, and stays for a while in standby - what can affect dramatically the battery usage when perform frequent network requests, so I'm scheduling alarm to perform the syncing with the server to be execute only once in 2 hours, or when the user explicitly using my application.

from the other hand, we would like to sync the collected data with the server in much shorter intervals if possible.

I was thinking that if I could know when the radio state become active from any reason (e.g when another application perform network operation) - it would be a good chance to perform my syncing, to take advantage the fact that the radio is anyway opened or in stand-by and consuming more power.

my questions:

I was expecting that the new JobScheduler introduced in android 5.0 will support scheduling jobs when network radio is active, but it does not seems to be the case according to the API. I believe that it will batch all jobs that requires network at the same time, but it will be good only in ideal world when all the applications on the device will use it wisely.

Upvotes: 2

Views: 815

Answers (2)

Dan
Dan

Reputation: 3725

I was curious about this same thing, and I finally found an answer here: http://android.xsoftlab.net/training/efficient-downloads/connectivity_patterns.html

The idea is to use ConnectivityManager like this:

connectivityManager.addDefaultNetworkActiveListener(new OnNetworkActiveListener() {
            @Override
            public void onNetworkActive() {
                // PREFETCH HERE
            }
});

I have yet to try it however.

Upvotes: 1

nico.ruti
nico.ruti

Reputation: 625

The short answer is no, you currently cannot determine the current state of the network adapter.

Android does not allow you to go that deep into the system. However, you could estimate the state on your own when watching the values in these four files:

/sys/devices/virtual/net/<InterfaceName>/statistics/tx_packets
/sys/devices/virtual/net/<InterfaceName>/statistics/rx_packets
/sys/devices/virtual/net/<InterfaceName>/statistics/tx_bytes
/sys/devices/virtual/net/<InterfaceName>/statistics/rx_bytes

You could read the current traffic of these files and map it to the appropriate state model of the used network adapter. However, the model varies heavily in Wifi, UMTS, LTE, etc. In this whitepaper of the PowerTutor developers (page 4), you can see the state-machine for Wifi and 3G with the transition conditions and timings (they have been found empirically).

In most cases, it's best to stick with the network usage tips provided by the Android developer pages. To be honest, I remember reading something about a network timer, too, but can't find the source anymore.

Edit: I found the missing source: Project Volta has been introduced with Android 5.0, offering you additional battery features.

Upvotes: 1

Related Questions