Reputation: 21
My Android app (running Android 10) uses network traffic statistics.
When my device is on Wifi, TrafficStats.getMobileTxBytes and ...getMobileRxBytes return always 0.
When it switches on Cellular network, returned values look good.
Any idea or solution?
Upvotes: 1
Views: 269
Reputation: 76516
You are using TrafficStats.getMobileTxByte
From the docs:
Return number of bytes transmitted across mobile networks since device boot. Counts packets across all mobile network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.
As you say:
return always 0. When it switches on Cellular network, returned values look good.
Yes that's because when the Cell Network is off, no mobile (cell) data is used, therefore 0.
You can use this to get the data used by the whole device:
val totalBytes = TrafficStats.getTotalRxBytes()
You can use the below if you want to get non-cellular traffic:
val nonMobileBytes = TrafficStats.getTotalRxBytes() - TrafficStats.getMobileTxBytes
https://developer.android.com/reference/android/net/TrafficStats#getTotalRxBytes()
Upvotes: 0