'Could not get traffic stats of tunnel interface in Android 10 & 11

Accessing /proc/net/dev is restricted from Android 10 (API level 29). And no luck for ifconfig as well.

I tried to use TrafficStats, in which we have restriction,

Introduced from API level 30:

TrafficStats.getTxPackets("tun0");
TrafficStats.getRxPackets("tun0");

Introduced from API level 31 (Android 12), so I cannot use this in API level 30 (Android 10)

TrafficStats.getTxBytes("tun0");
TrafficStats.getRxBytes("tun0");

So could not get both bytes and packets in Android 11. And NetworkStatsManager TRANSPORT_VPN doesn't work.

Next I have tried to write a C code, used getifaddrs but ifa_data value is null (not used in struct)

Tried ioctl also but no luck.

Kindly help to get traffic stats in Android 10 and Android 11.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source