How to get WIFI signal strength

kevin david 161 Reputation points
2022-06-14T22:51:21.22+00:00
public int GetStrenght()  
        {  
            Context _context = Android.App.Application.Context;  
  
  
            WifiManager wifiManager = null;  
            wifiManager = (WifiManager)_context.GetSystemService(Context.WifiService);  
  
            if (wifiManager == null)  
            {  
                return 2022;  
            }  
  
            int numberOfLevels = 5;  
  
#pragma warning disable CS0618 // Type or member is obsolete  
            WifiInfo wifiInfo = null;  
            wifiInfo= wifiManager.ConnectionInfo;  
#pragma warning restore CS0618 // Type or member is obsolete  
  
            if (wifiInfo == null)  
            {  
                return 2022;  
            }  
  
#pragma warning disable CS0618 // Type or member is obsolete  
            int level = WifiManager.CalculateSignalLevel(wifiInfo.Rssi, numberOfLevels);  
#pragma warning restore CS0618 // Type or member is obsolete  
  
            return level;  
  
        }  

--It gives me a NullReferenceException error and I don't know why. In this Line wifiInfo= wifiManager.ConnectionInfo;
Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,818 questions
No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 41,651 Reputation points Microsoft Employee
    2022-06-15T01:34:51.397+00:00

    Hello,​

    Please add <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> in AndroidManifest.xml, then you can get the wifi signal strength

    For more information about this permission, you can refer to this document: https://developer.android.com/reference/android/Manifest.permission#ACCESS_WIFI_STATE

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Asgar Ali 26 Reputation points
    2022-06-16T18:18:07.893+00:00

    In Addition to what LeonLu suggested try initialising wifiManager like this.

     WifiManager wifiManager = WifiManager.FromContext(Android.App.Application.Context);