Network analysis tools for Android - Speedtest API

(back)

Prerequisites

Your device must be rooted to use this API, because it installs a binary that does the actual network measurement, and this binary will eventually require root privileges to do passive monitoring, similar to the TrafficStats per-application monitor.

Setup

Before using the API in your application, follow these setup steps:
  1. Unpack the archive file to any location you wish.
  2. Open Eclipse.
  3. Import the netstatslib library project by selecting File->Import and navigating to the location of the unpacked archive contents.
  4. Open your Android project, right-click on the project, and select "Properties."
  5. Under the "Android" section, add netstatslib as a library. (Note that, due to the way library projects work in the Android build system, this is not the same as adding the netstatslib folder to your project's build path.)
  6. Add the RootTools JAR file, located in netstatslib/libs, to your project's build path. (This step is also necessitated by the nature of the Android build process, as above.)

Usage (JavaDoc)

Here is a very simple example of using the API in an Android application:

            import edu.umich.mobility.netstatslib.BaseEstimator;
            import edu.umich.mobility.netstatslib.SpotEstimator;
            import edu.umich.mobility.netstatslib.NetworkStats;

            public class MyActivity extends Activity {
                private SpotEstimator estimator;

                public void onCreate(Bundle savedInstanceState) {
                    // this function must be called before any others
                    BaseEstimator.init(this);
                    estimator = SpotEstimator.getInstance("eth0");
                }
            
                // ...irrelevant details omitted

                public void onButtonPressed() {
                    NetworkStats stats = estimator.getStats();
                    // do something cool with stats
                }
            }
        
Alternatively, here is an example using the publish/subscribe API:
            import edu.umich.mobility.netstatslib.BaseEstimator;
            import edu.umich.mobility.netstatslib.SpotEstimator;
            import edu.umich.mobility.netstatslib.NetworkStats;
            import edu.umich.mobility.netstatslib.OnUpdateListener;

            public class MyActivity extends Activity {
                private SpotEstimator estimator;

                private OnUpdateListener listener = new OnUpdateListener() {
                    public void onUpdate(NetworkStats update) {
                        // do something cool with the update
                    }
                };

                public void onCreate(Bundle savedInstanceState) {
                    // this function must be called before any others
                    BaseEstimator.init(this);
                    estimator = SpotEstimator.getInstance("eth0");
                    estimator.subscribe(listener);
                }

                public void onDestroy() {
                    estimator.unsubscribe(listener);
                }
            }
        
See the NetStatsLib JavaDoc for further details. Additionally, there is a simple but complete example (resembling the above code, but in a different context) in the tests/ folder of netstatslib.