13

I am building an application reading the signal strength of each available Wifi access point.

I've written code like:

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    // Get WiFi status
    WifiInfo info = wifi.getConnectionInfo();
    textStatus.append("\n\nWiFi Status: " + info.toString());

    // List available networks
    List<WifiConfiguration> configs = wifi.getConfiguredNetworks();

However, I have two problems:

  1. In debugging, configs only contains one connection. However, I can see that there are several APs available in the system's wifi setting. I.e. configs is an incomplete list.

  2. I don't know how to get the signal strength in WifiConfiguration.

btw, I am using HTC Hero and Android 1.5.

Yin Zhu
  • 16,980
  • 13
  • 75
  • 117

5 Answers5

17

According to the Android API documentation WifiManager.getConfiguredNetworks() does not fill the signal strength paramters. This data only represents the remembered access point settings, not the visible ones.

To get actually visible networks you must call WifiManager.startScan() to initiate WiFi radio scanning and WifiManager.getScanResults() after a while to get the scanning results.

Sergii Rudchenko
  • 5,170
  • 2
  • 28
  • 24
  • 1
    How long do you exactly mean by "a while"? Isn't there any blocking call to this API method? – Hosane Jun 19 '12 at 17:26
  • 3
    It's usually 2-10 seconds (but no restrictions are implied really). But keep in mind, you may miss some visible access points if you scan for too short. Wi-Fi access points are detected by special broadcast packets (so called "beacons") which are emitted periodically. Scanning for access points actually means listening for the beacons and collecting them. http://www.wi-fiplanet.com/tutorials/article.php/1492071 – Sergii Rudchenko Jun 20 '12 at 06:40
  • There is no synchronous function for the whole process. Though you may just call getScanResults() to get results of a previous scan performed by the system. Maybe that results are fresh enough for your purpose. However I won't expect it to be good if you have an active WiFi connection - I think the Android system components don't run scans when a good connection is present. There is just no reason to do that. – Sergii Rudchenko Jun 20 '12 at 06:44
  • Thanks for the replies, I am actually writing a simple app to record wifi perception around the site. So, user has a single button and as he/she presses the button wifi access point perceptions are recorded into a database. With such a mechanism, on every button press the user should stand still for a few seconds. – Hosane Jun 22 '12 at 17:12
  • Calling a synchronous function isn't a good solution anyway. Android will show the ANR (Application Not Responding) warnings for your application if you block for too long in the UI thread. Consider disabling the button during a scan process, that fits the event-based UI model better. – Sergii Rudchenko Jun 25 '12 at 20:51
  • Great tips friend! I have set it to something in middle, something about 5 secs and looks like it is working perfectly... – Hosane Jul 09 '12 at 01:28
7

below code will help to get bar of wifi:

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));
AJit
  • 1,283
  • 1
  • 13
  • 34
3
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
    registerReceiver(receiverWifi, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {

        List<ScanResult> wifiList = mainWifi.getScanResults();
        ArrayList<WifiConnectionBean> m4MessagesList = new ArrayList<WifiConnectionBean>();
        for (int i = 0; i < wifiList.size(); i++) {
            ScanResult scanResult = wifiList.get(i);
            WifiConnectionBean bean = new WifiConnectionBean();
            bean.setConnectionName(scanResult.SSID); // + "--" +
                                                        // scanResult.frequency);
            bean.setDescription(scanResult.capabilities);
            bean.setId(scanResult.SSID);
            bean.setLevel(String.valueOf(scanResult.level));
            bean.setSignalStrength(String.valueOf(scanResult.BSSID));
            m4MessagesList.add(bean);
        }
        if (m4MessagesList == null) {
            Toast.makeText(WifiIdentificationActivity.this,
                    "WifiConnection not available", Toast.LENGTH_SHORT)
                    .show();
        } else {
            String message = "Scanning complete. " + m4MessagesList.size()
                    + " connections found!";
        }
        pd.dismiss();

    }
}

where scanResult.SSID gives the signal strength.

Asad Rao
  • 3,190
  • 1
  • 22
  • 26
  • I don't think either BSSID or SSID gives the signal strength. SSID returns the access point name while BSID returns the address of the access point. https://developer.android.com/reference/android/net/wifi/ScanResult.html – danijax Nov 16 '17 at 19:43
1
private void checkNetworkStrengh() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo Info = cm.getActiveNetworkInfo();
        if (Info == null || !Info.isConnectedOrConnecting()) {
            Log.i(TAG, "No connection");
        } else {
            int netType = Info.getType();
            int netSubtype = Info.getSubtype();

            if (netType == ConnectivityManager.TYPE_WIFI) {
                Log.i(TAG, "Wifi connection");
                WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
                List<ScanResult> scanResult = wifiManager.getScanResults();
                for (int i = 0; i < scanResult.size(); i++) {
                    Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal 
                }


                // Need to get wifi strength
            } else if (netType == ConnectivityManager.TYPE_MOBILE) {
                Log.i(TAG, "GPRS/3G connection");
                // Need to get differentiate between 3G/GPRS
            }
        }
    }
Issac Balaji
  • 1,441
  • 1
  • 13
  • 25
1
    WifiManager wifiManager = (WifiManager)
    MonitorActivity.this.getSystemService(Context.WIFI_SERVICE);

    wifiManager.addNetwork(conf);

    int numberOfLevels = 5;
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);

    Log.e("level", "" + level);
    // you will get the levels. Using these levels you can calculate strenghts.
Junior Zancan
  • 25
  • 1
  • 13
arshad shaikh
  • 673
  • 8
  • 11