Is there a way to count the data consumed and transmitted via WiFi/LAN in Android? I’m able to check the statistics for mobile internet (3G, 4G) via the TrafficStats methods getMobileTxBytes() and getMobileRxBytes(), but how about WiFi?
Is there a way to count the data consumed and transmitted via WiFi/LAN in
Share
UPDATE: The original answer, below, is most likely WRONG. The numbers I’m getting for WiFi/LAN are way too high. Still haven’t figured why (it seems measuring traffic via WiFi/LAN isn’t possible), but an old question offers some insight: How to get the correct number of bytes sent and received in TrafficStats?
Found my own answer.First, define a method called getNetworkInterface(). I don’t know exactly what a “network interface” is but we need the String token it returns to build the path to the file containing the byte count.
Next, define readLongFromFile(). We will actually have two file paths–one for the bytes sent, and one for bytes received. This method simply encapsulates reading the file path supplied to it and returns the count as a long.
Lastly, build the methods that return the number of bytes sent and received via WiFi/LAN.
Now, we can test our methods by doing something like our example for mobile internet above.