loading...
Mostrando entradas con la etiqueta Android Wifi. Mostrar todas las entradas
Mostrando entradas con la etiqueta Android Wifi. Mostrar todas las entradas

martes, 27 de diciembre de 2011

Android. Determinar la velocidad de la red durante la reproducción de un vídeo.

Leer cantidad de bytes desde la red wifi.

private long parserNumber(String line) throws Exception {
        long ret=0;
        String[] delim = line.split(" ");
        if(delim.length >= 1){
            ret = Long.parseLong(delim[0]);
        }
        return ret;
    }
   
    public long syncFetchReceivedBytes() {
        // TODO Auto-generated method stub
        ProcessBuilder cmd;
        long readBytes=0;
        BufferedReader rd = null;
        try {
            String[] args = { "/system/bin/cat", "/proc/net/dev" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            rd = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line;
            int linecount = 0;
            while ((line = rd.readLine()) != null) {
                linecount++;
                if(line.contains("lan0")||line.contains("eth0")){
                    String[] delim = line.split(":");
                    if(delim.length>=2){
                        readBytes=parserNumber(delim[1].trim());
                        break;
                    }
                }
            }
            rd.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return readBytes;
    }

 

Invocando la función anterior cada segundo

long curReadBytes=syncFetchReceivedBytes();
String strSpeed=(curReadBytes-lastReadBytes)/1024 kbps
lastReadBytes = curReadBytes;