Android: difference between data usage on a hotspot phone and on a phone using it

239 views Asked by At

I am trying to compare the data usage difference between a phone using an other phone's hotspot and the hotspot of the phone.

On the phone having its hotspot turned on, I am using this code to compute the data usage of the hotspot(The result is display on a TextView (TextView) findViewById(R.id.data_seller)). I named this phone the server phone:

private void getNetworkStatsServer() {
    NetworkStatsManager networkStatsManager;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        networkStatsManager = getApplicationContext().getSystemService(NetworkStatsManager.class);
        NetworkStats networkStatsWifi = null;
        NetworkStats networkStatsMobile = null;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            if (networkStatsManager != null) {
                networkStatsWifi = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI,
                        "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                String suscribeId = "";
                TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null) {
                        suscribeId = tm.getSubscriberId();
                }
                networkStatsMobile = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE,
                        suscribeId, 0, calendar.getTimeInMillis(), UID_TETHERING);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        NetworkStats.Bucket bucket;

        if (networkStatsWifi != null) {
            while (networkStatsWifi.hasNextBucket()) {
                bucket = new NetworkStats.Bucket();
                networkStatsWifi.getNextBucket(bucket);
                mStartTXServer += bucket.getTxBytes();
                mStartRXServer += bucket.getRxBytes();
            }
        }

        if (networkStatsMobile != null) {
            while (networkStatsMobile.hasNextBucket()) {
                bucket = new NetworkStats.Bucket();
                networkStatsMobile.getNextBucket(bucket);
                mStartTXServer += bucket.getTxBytes();
                mStartRXServer += bucket.getRxBytes();
            }
        }
    }
    mHandler.postDelayed(mRunnableServer, 1000);
}

mRunnableServer = new Runnable() {
        public void run() {
            long[] res = new long[2];
            NetworkStatsManager networkStatsManager;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                networkStatsManager = getApplicationContext().getSystemService(NetworkStatsManager.class);
                NetworkStats networkStatsWifi = null;
                NetworkStats networkStatsMobile = null;
                try {
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DATE, 1);
                    if (networkStatsManager != null) {
                        networkStatsWifi = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI,
                                "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                        networkStatsMobile = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE,
                                "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                NetworkStats.Bucket bucket;

                if (networkStatsWifi != null) {
                    while (networkStatsWifi.hasNextBucket()) {
                        bucket = new NetworkStats.Bucket();
                        networkStatsWifi.getNextBucket(bucket);
                        res[0] += bucket.getTxBytes();
                        res[1] += bucket.getRxBytes();
                    }
                }
                if (networkStatsMobile != null) {
                    while (networkStatsMobile.hasNextBucket()) {
                        bucket = new NetworkStats.Bucket();
                        networkStatsMobile.getNextBucket(bucket);
                        res[0] += bucket.getTxBytes();
                        res[1] += bucket.getRxBytes();
                    }
                }
                if (networkStatsMobile != null || networkStatsWifi != null) {
                    res[0] -= mStartTXServer;
                    res[1] -= mStartRXServer;
                }
            } else {
                res[0] = TrafficStats.getUidTxBytes(UID_TETHERING) - mStartTXServer;
                res[1] = TrafficStats.getUidRxBytes(UID_TETHERING) - mStartRXServer;
            }

            System.out.println("Value of Rx: " + res[0]);
            System.out.println("Value of Tx: " + res[1]);

                ((TextView) findViewById(R.id.data_seller)).setText(String.valueOf(((double) (res[0] + res[1])) / 1048576) + "MB");
                mHandler.postDelayed(mRunnableServer, 10000);
        }
    };

Concerning the phone using the hotspot to be connected on internet, I compute the total data usage of the Wifi.I named this phone the client phone

private void getNetworkStatsClient() {
    mStartTXClient = TrafficStats.getTotalTxBytes();
    mStartRXClient = TrafficStats.getTotalRxBytes();

    mHandler.postDelayed(mRunnableClient, 1000);
}

mRunnableClient = new Runnable() {
        public void run() {
            long[] res = new long[2];
            res[0] = TrafficStats.getTotalTxBytes() - mStartTXClient;
            res[1] = TrafficStats.getTotalRxBytes() - mStartRXClient;

            System.out.println("Value of Rx: " + res[0]);
            System.out.println("Value of Tx: " + res[1]);

            ((TextView) findViewById(R.id.data_buyer)).setText(String.valueOf(((double) (res[0] + res[1])) / 1048576) + "MB");
            mHandler.postDelayed(mRunnableClient, 10000);
        }
    };

I thought the result of both would have been more or less the same (More precisely, res[0]+res[1] in both runnable would have been more or less equal) because the client phone is using the server hotspot phone. However, the results are totally different (The data usage of the client phone is 50 times greater than the server phone). Do you have an idea why?

1

There are 1 answers

0
hartmut On

Let me try to rephrase.

Set up: You have an AP and some Users. That is regular tethering.

Goal: You want to measure the data/bandwidth usage. I.e., how much data did the Users suck.

Experiment: You try to measure this usage on the AP side as well as on the User's side.

Observation: You are surprised to see that what you measure on the AP side is not the same as what you measure on the Users side.

Potential investigation strategy:

I see you have and if-else depending on the SDK version. Say that when experimenting, your AP is always the same device, and the User is always another device, then each side uses may use a different API.

Have you tried running your code on two devices that have the same SDK, and will therefore use the same API? This isn't an answer, but it might be informative.