Frage

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?

War es hilfreich?

Lösung 2

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.

private String getNetworkInterface() {
    String wifiInterface = null;
    try {
        Class<?> system = Class.forName("android.os.SystemProperties");
        Method getter = system.getMethod("get", String.class);
        wifiInterface = (String) getter.invoke(null, "wifi.interface");
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (wifiInterface == null || wifiInterface.length() == 0) {
        wifiInterface = "eth0";
    }
    return wifiInterface;
}

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.

private long readLongFromFile(String filename) {
    RandomAccessFile f = null;
    try {
        f = new RandomAccessFile(filename, "r");
        String contents = f.readLine();
        if (contents != null && contents.length() > 0) {
            return Long.parseLong(contents);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (f != null) try { f.close(); } catch (Exception e) { e.printStackTrace(); }
    }
    return TrafficStats.UNSUPPORTED;
}

Lastly, build the methods that return the number of bytes sent and received via WiFi/LAN.

private long getNetworkTxBytes() {
    String txFile = "sys/class/net/" + this.getNetworkInterface() + "/statistics/tx_bytes";
    return readLongFromFile(txFile);
}

private long getNetworkRxBytes() {
    String rxFile = "sys/class/net/" + this.getNetworkInterface() + "/statistics/rx_bytes";
    return readLongFromFile(rxFile);
}

Now, we can test our methods by doing something like our example for mobile internet above.

long received = this.getNetworkRxBytes();
long sent = this.getNetworkTxBytes();

if (received == TrafficStats.UNSUPPORTED) {
    Log.d("test", "TrafficStats is not supported in this device.");
} else {
    Log.d("test", "bytes received via WiFi/LAN: " + received);
    Log.d("test", "bytes sent via WiFi/LAN: " + sent);
}

Andere Tipps

(This is actually a comment for your answer, don't have enough points to really comment, yet...) TrafficStats.UNSUPPORTED does not necessarily mean that the device doesn't support reading the WiFi traffic stats. In case of my Samsung Galaxy S2, the file which contains the stats does not exist, when WiFi is disabled, but it works, when WiFi is enabled.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top