Orangenlimo
Orangenlimo

Reputation: 11

Android TrafficStats getTotalRxBytes() returns always zero

Im trying to measure how much bytes my app has received. Im doing it this way:

long receivedBytesBefore = TrafficStats.getTotalRxBytes();
...
doSomething();
...
long receivedBytesAfter = TrafficStats.getTotalRxBytes();
long receivedBytes = receivedBytesAfter - receivedBytesBefore;

My problem is that getTotalRxBytes() always returns 0. So my result is 0 no matter what I do. I have found out that the method is just reading some textfiles like

/sys/class/net/rmnet0/statistics/rx_bytes

So I looked into these files and they all contain just "0".

Do I miss something or do I have to activate this function somehow? Is there another way to measure how much bytes my app has received?

My Android device is a Samsung Galaxy Ace S5830 running Android 2.3.3

Upvotes: 1

Views: 3930

Answers (3)

Jorgesys
Jorgesys

Reputation: 126493

You will have a read of 0 bytes, because you cannot access to this directories:

"/sys/class/net/rmnet0/statistics/tx_bytes"
"/sys/class/net/ppp0/statistics/tx_bytes"
"/sys/class/net/rmnet0/statistics/rx_bytes"
"/sys/class/net/ppp0/statistics/rx_bytes"

Nowadays you only need to read the values using the class TrafficStats:

Log.i(TAG, MobileTxBytes: ${TrafficStats.getMobileTxBytes()}")
Log.i(TAG, "MobileRxBytes: ${TrafficStats.getMobileRxBytes()}")
Log.i(TAG, "MobileTxPackets: ${TrafficStats.getMobileTxPackets()}")
Log.i(TAG, "MobileRxPackets: ${TrafficStats.getMobileRxPackets()}")

Upvotes: 0

Dave
Dave

Reputation: 3208

I can verify this was happening to me as well.

From behavior I've observed, it appears that getTotalRxBytes only works when wifi is connected. But something to be aware of is that if you are trying to get an accurate number of bytes received for a file for example, there seams to be some extra bytes sent.

So if you don't need it to be super accurate. You can use getMobileRxBytes() for when wifi is not active and getTotalRxBytes() for when wifi is active.

Here is a simple example.

ie:

    ConnectivityManager connManager;
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    long initialBytes = 0;
    long finalBytes = 0;
    long byteDifference = 0;
    boolean onWifi= false;

    if (mWifi.isConnected())
    {
     //wifi connected
     initialBytes = TrafficStats.getTotalRxBytes();
     onWifi = true;
    }
    else if (mMobile.isConnected()) 
    {
    //if 3g/4g connected
     initialBytes = TrafficStats.getMobileRxBytes();
     onWifi = true;
    }
    else
    {
     //Something funny going on
     Log.e("Error", "Something funny going on");
     return;
    }


// Process whatever you want to process


    if(onWifi)
    {
      finalBytes = TrafficStats.getTotalRxBytes();
    }
    else
    {
      finalBytes = TrafficStats.getMobileRxBytes();
    }

    byteDifference  = finalBytes - initialBytes;

Something along these lines. Hopefully this helps.

Upvotes: 3

P Varga
P Varga

Reputation: 20249

These may not be supported on your device, and can return UNSUPPORTED, which might be 0.

Upvotes: -1

Related Questions