Search code examples
xamarin.android

How to get a list of names of Wi-Fi network


How to get a list of names of Wi-Fi network and signal

I used the following method to fill the array. It works on Android versions less than 5 only. and does not work on 6 or higher. I want code that works on all versions.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

List myListrow = new List();

        var wifiMgr = (WifiManager)GetSystemService(WifiService);
        var wifiList = wifiMgr.ScanResults;

        foreach (var item in wifiList)
        {
            var wifiLevel = WifiManager.CalculateSignalLevel(item.Level, 100);
             myListrow.Add(($"Wifi Name: {item.Ssid} - Single: {wifiLevel}"));

        }

Solution

  • Update:

    Add the permission.ACCESS_COARSE_LOCATION permission. The original code works well.

    The code used to list the all the names:

    void getWifiList() {
    
            IList myListrow = new ArrayList();
    
            var wifiMgr = (WifiManager)GetSystemService(WifiService);
            var wifiList = wifiMgr.ScanResults;
    
            foreach (var item in wifiList)
            {
                var wifiLevel = WifiManager.CalculateSignalLevel(item.Level, 100);
                myListrow.Add(($"Wifi Name: {item.Ssid} - Single: {wifiLevel}"));
    
            }
    
        }
    

    The code to request permission:

     private void RequestPermission()
        {
            Log.Info(TAG, " permission has NOT been granted. Requesting permission.");
    
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessCoarseLocation))
            {
    
                Snackbar.Make(layout, Resource.String.permission_accesscoarselocation_rationale,
                    Snackbar.LengthIndefinite).SetAction(Resource.String.ok, new Action<View>(delegate (View obj) {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessCoarseLocation }, REQUEST_LOCATION);
                    })).Show();
            }
            else
            {
                // AccessCoarseLocation permission has not been granted yet. Request it directly.
                ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessCoarseLocation }, REQUEST_LOCATION);
            }
        }
    

    Screenshot:

    enter image description here

    You could download the source file from the link below. https://github.com/WendyZang/Test/tree/master/Wi-Fi%20network/GetNetWorkInfo