title: How to get signal strength of connected WiFi android?
body: <p>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?</p>
  
  <p>I tried this code. But it doesn't work.</p>
  
  <pre><code>    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&lt;ScanResult&gt; 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 &gt;= 100)
                          signalStrangth = 4;
                      else if(difference &gt;= 75)
                          signalStrangth = 3;
                      else if(difference &gt;= 50)
                          signalStrangth = 2;
                      else if(difference &gt;= 25)
                          signalStrangth = 1;
                      tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth);
  
                  }
  
              }
          }
      }
  }, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
  </code></pre>
  
  <p>I also tried this code. But this is doesn't work too.</p>
  
  <pre><code>    int numberOfLevels = 5;
      WifiInfo wifiInfo = wifiManager.getConnectionInfo();
      int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
  </code></pre>