0

Is there an up-to-date way to get the current WiFi signal strength in Xamarin.Forms for an Android device?

The code snippet following adapted from article 58191523 says that:

The name 'GetSystemService' does not exist in the current context

The name 'WifiService' does not exist in the current context

'WiFiManager.CalculateSignalLevel(int, int)' is obsolete: 'deprecated'

using Android.Net.Wifi;        
public static void SignalStrengthToConsole()
{
    var wifiMgr = (WifiManager)GetSystemService(WifiService);
    var wifiList = wifiMgr.ScanResults;
    foreach (var item in wifiList)
    {
        var wifiLevel = WifiManager.CalculateSignalLevel(item.Level, 100);
        Console.WriteLine($"Wifi SSID: {item.Ssid} - Strengh: {wifiLevel}");
    }
}
  • are you implementing this code in the Android project? – Jason Jan 11 '23 at 15:43
  • Check Xamatin.Essentials - an int prop there for rough strength: https://github.com/xamarin/Essentials/issues/480 – Shaw Jan 11 '23 at 19:43
  • Jason: Yes. The context issues can be resolved by saving the context from MainActivity, i.e. passing it into the class and saving a reference. The deprecation warning persists. – DumbestGuyOnSaturn Jan 11 '23 at 20:19
  • It sounds like you've resolved the errors, and now only have a question about the deprecation warning? If so, please update your question to indicate what you need help with. OR if you don't care about the deprecation, and consider this to be resolved, add "Your Answer" below, showing the code that solves the errors. – ToolmakerSteve Jan 12 '23 at 04:02
  • Will post working code once we get it working. Right now wifiMgr.ScanResults is returning an empty list and we're working on determining why. Also, it appears that there is no Xamarin.Essentials support because there is no iOS capability. – DumbestGuyOnSaturn Jan 12 '23 at 14:35

1 Answers1

0

Here are the steps to retrieving the current Wi-Fi signal strength using the deprecated capabilities:

Step 1: On the Android device, tap

Settings > Location

then tap

App permission

Scroll down until you find your App.

Tap your app and select any option other than "Deny".

Step 2: Ensure you have the following in your project AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" />

Step 3: Include the following code in the class where you wish to capture the Wi-Fi signal strength using the deprecated capabilities:

using System;
using System.Linq;
namespace YourNamespaceHere
{
    public static class Device
    {
        private static Android.Net.Wifi.WifiManager deviwifimanager;
        private static Android.Content.Context devicontext;
        public static void PassContext(Android.Content.Context context00)
        {
            devicontext = context00;
            deviwifimanager = (Android.Net.Wifi.WifiManager)devicontext.GetSystemService(Android.Content.Context.WifiService);
            devwifimanager.StartScan();
        }
        public static Int32 SignalStrength()
        {
            Int32 temp00 = 0;
            string text00 = null;
            try
            {
                var wifilist = deviwifimanager.ScanResults;
                foreach (var wifilistitem in wifilist)
                {
                    temp00 = Android.Net.Wifi.WifiManager.CalculateSignalLevel(wifilistitem.Level, 100);
                    text00 = wifilistitem.Ssid;
                    // add your code to process signal level and SSID as required
                }
            }
            catch
            {
                temp00 = 0;
            }
            return temp00;
        }
    }
}

Step 4: Add the following code in the OnCreate method in MainActivity.cs:

Device.PassContext(this);