Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get signal strength of connected WiFi android?

I want to get signal strength of this wifi which is connected with my device. This is probably easy. But I am beginner. And It's homework. How to get it?

I tried this code. But it doesn't work.

    registerReceiver(new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        int state = wifi.getWifiState();
        if(state == WifiManager.WIFI_STATE_ENABLED) {
            List<ScanResult> results = wifi.getScanResults();

            for (ScanResult result : results) {
                if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) {
                    int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(),
                            result.level);
                    int difference = level * 100 / result.level;
                    int signalStrangth= 0;
                    if(difference >= 100)
                        signalStrangth = 4;
                    else if(difference >= 75)
                        signalStrangth = 3;
                    else if(difference >= 50)
                        signalStrangth = 2;
                    else if(difference >= 25)
                        signalStrangth = 1;
                    tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth);

                }

            }
        }
    }
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));

I also tried this code. But this is doesn't work too.

    int numberOfLevels = 5;
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
like image 581
Emrah Şentürk Avatar asked Sep 16 '13 15:09

Emrah Şentürk


1 Answers

Hope it works..

public void onReceive(WifiManager wifiManager) {
                      int numberOfLevels=5;
                      WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                      int level=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
                      System.out.println("Bars =" +level);
                }
like image 165
user3135727 Avatar answered Sep 22 '22 22:09

user3135727