在Android中显示WiFi信号强度(显示栏)

时间:2015-04-17 19:23:28

标签: android listview distance android-wifi signal-strength

我已经申请获取并显示相关的wifi信息,删除重复的SSID并通过信号强度显示它们(更高的RSSI优先),同时获得的RSSI我正在计算每个AP的近似距离,我会想知道是否有办法在带有颜色代码的栏上显示我的RSSI(如Wifi Manager App)。我在ListView中显示所有内容。这是我的代码。

public void onReceive(Context c, Intent intent) {


        //////////////////// Inicializacion de variables /////////////////////
        final ImageView fondo=(ImageView)findViewById(R.id.fondo);
        ListaWifi = ObjWifi.getScanResults();
        rssi = new int[ListaWifi.size()];
        wifisaux = new String[ListaWifi.size()];
        wifis = new String[ListaWifi.size()];
        BSSID=new String[ListaWifi.size()];
        tam = ListaWifi.size();
        ///////////////// Creacion de Lista de AP /////////////////////////
        for (int i = 0; i <tam; i++) {

            ////////// Asignacion de canales de acuerdo a frec /////////
            switch (ListaWifi.get(i).frequency) {
                case 2412:
                    canal = 1;
                    break;
                case 2417:
                    canal = 2;
                    break;
                case 2422:
                    canal = 3;
                    break;
                case 2427:
                    canal = 4;
                    break;
                case 2432:
                    canal = 5;
                    break;
                case 2437:
                    canal = 6;
                    break;
                case 2442:
                    canal = 7;
                    break;
                case 2447:
                    canal = 8;
                    break;
                case 2452:
                    canal = 9;
                    break;
                case 2457:
                    canal = 10;
                    break;
                case 2462:
                    canal = 11;
                    break;
            }
            //////////Calculo de la distancia ///////////////////

            dist = Math.pow(10, (-(-26) - (ListaWifi.get(i).level)) / (10 * 21.54));

            /////////////Agregacion de valores en la lista ////////////////
            BSSID[i]=ListaWifi.get(i).BSSID;
            rssi[i] = ListaWifi.get(i).level;
            nombre=BSSID[0];
            wifisaux[i] = (("AP " + (i + 1) + "\n" + "SSID: " + ListaWifi.get(i).SSID + "\n BSSID: " + ListaWifi.get(i).BSSID + "\n Canal: " + canal + "\n RSSI: " + rssi[i] + "dBm" + "\n Distancia: " + dist + " m").toString());

        }

        ///////////////Ordenamiento de AP de acuerdo a potencia///////Ordering list (higher RSSI first)///////
        for (int i = 0; i < tam-1; i++) {
            if (rssi[i] > rssi[i+1]) {
            }else{
                mascerca = wifisaux[i];
                wifisaux[i] = wifisaux[i+1];
                wifisaux[i+1] = mascerca;
                auxbssid=BSSID[i];
                BSSID[i]=BSSID[i+1];
                BSSID[i+1]=auxbssid;
            }
        }
        //////////////Eliminacion de redundancias//////Deleting duplicate SSIDs/////////////////////
        for (int i=0;i<tam;i++){
            for (int j = 1; j < tam; j++) {
                if (BSSID[i].equals(BSSID[j])){
                    wifisaux[i]=wifisaux[j];
                    BSSID[i]=BSSID[j];
                }
            }
            wifis[i] = wifisaux[i];
        }


        ////////////Cambio de imagenes ///////////////////////////////
        nombre=BSSID[0];
        if (nombre.equals(nombre2)){

        }
        else {
            final String str="img_"+(rnd.nextInt(6)+1);
            fondo.setImageDrawable(getResources().getDrawable(getResourceID(str, "drawable",getApplicationContext())));
        }
        nombre2=BSSID[0];

        /////////////Traslado de variables a Lista ////////Show everything in a list//////////////
        list.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, wifis));
    }

1 个答案:

答案 0 :(得分:0)

我也很想知道如何在无线信号中显示图标。

您可以更改:

////////// Asignacion de canales de acuerdo a frec /////////
        switch (ListaWifi.get(i).frequency) {
            case 2412:
                canal = 1;
                break;
            case 2417:
                canal = 2;
                break;
            case 2422:
                canal = 3;
                break;
            case 2427:
                canal = 4;
                break;
            case 2432:
                canal = 5;
                break;
            case 2437:
                canal = 6;
                break;
            case 2442:
                canal = 7;
                break;
            case 2447:
                canal = 8;
                break;
            case 2452:
                canal = 9;
                break;
            case 2457:
                canal = 10;
                break;
            case 2462:
                canal = 11;
                break;
        }

为:

    in freq = ListaWifi.get(i).frequency;
    canal = (freq == 2484) ? 14 : ((freq -2407) / 5);