Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

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

monemdcmonemdc Member ✭✭✭
edited June 2020 in Xamarin.Android

hi

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




`
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}"));

        }

`

Tagged:

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    Maybe you faced a permission issue. An app that targets Android 6.0 or higher must always perform a runtime permission check.
    You can refer to the following link to add a runtime permission check.
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=vswin

    Besides, you can use the package Permissions.Plugin and add in your Activity:

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    

    For more details,you can check:
    https://github.com/jamesmontemagno/PermissionsPlugin

    Note:Remember to add necessary permissions:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    Maybe you faced a permission issue. An app that targets Android 6.0 or higher must always perform a runtime permission check.
    You can refer to the following link to add a runtime permission check.
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=vswin

    Besides, you can use the package Permissions.Plugin and add in your Activity:

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    

    For more details,you can check:
    https://github.com/jamesmontemagno/PermissionsPlugin

    Note:Remember to add necessary permissions:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
  • monemdcmonemdc Member ✭✭✭

    hi jezh
    i add permission
    Otherwise, if I had not added them, nothing would have appeared with me on issues under 5.

    The error codes used are not valid for higher 6 versions

  • jezhjezh Member, Xamarin Team Xamurai

    Have you added the runtime permission?

  • monemdcmonemdc Member ✭✭✭

    hi
    jezh

    Have you added the runtime permission? yes

  • jezhjezh Member, Xamarin Team Xamurai
    edited June 2020

    I created a simple demo, and added runtime permission for permission.ACCESS_COARSE_LOCATION, it works properly.
    The result is:

    You can check the full demo in the attachment.

    Hope it can help you.

  • monemdcmonemdc Member ✭✭✭

    thanks jezh

  • MikasMikas Member

    Hi jezh, I tried your demo on my HUAWEI 10 (Android 7.0-API24) and the list is empty. Can you help me?

Sign In or Register to comment.