Maria Nabil
Maria Nabil

Reputation: 149

How to get the connected SSID in Xamarin.Forms?

I need to get the SSID of the current network that I am connected to.

Here is the code I used to find the SSID in Xamarin.Android:

WifiManager wifiManager = (WifiManager)(Application.Context.GetSystemService(WifiService));

if (wifiManager != null)
{
    var ssid =  wifiManager.ConnectionInfo.SSID;
}
else
{
    var str =  "WiFiManager is NULL";
}

But I need to implement this in Xamarin.Forms.

How can I do this ?

Upvotes: 1

Views: 5082

Answers (2)

SmartHouse Coder
SmartHouse Coder

Reputation: 19

Get SSID and BSSID API31 Xamarin C# Example

Required permissions: CHANGE_NETWORK_STATE, ACCESS_FINE_LOCATION

If API<31 TransportInfo will return Null

using Android.Content;
using Android.Net;
using Android.Net.Wifi;

protected override void OnStart()
    {
        base.OnStart();

        NetworkRequest request = new NetworkRequest.Builder().AddTransportType(transportType: TransportType.Wifi).Build();
        ConnectivityManager connectivityManager = Android.App.Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;

        NetworkCallbackFlags flagIncludeLocationInfo = NetworkCallbackFlags.IncludeLocationInfo;
        NetworkCallback networkCallback = new NetworkCallback((int)flagIncludeLocationInfo);
        connectivityManager.RequestNetwork(request, networkCallback);
    }

    private class NetworkCallback : ConnectivityManager.NetworkCallback
    {
        public NetworkCallback(int flags) : base(flags)
        {
        }

        public override void OnCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities)
        {
            base.OnCapabilitiesChanged(network, networkCapabilities);
            WifiInfo wifiInfo = (WifiInfo)networkCapabilities.TransportInfo;

            if (wifiInfo != null)
            {
                string ssid = wifiInfo.SSID.Trim(new char[] {'"', '\"' });
                string bssid = wifiInfo.BSSID;
            }
        }
    }

Click Android API reference.ConnectivityManager.NetworkCallback(int)!

Upvotes: 0

Ricardo Dias Morais
Ricardo Dias Morais

Reputation: 2087

You can use DependencyService.

The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.

Create a public interface (for organization sake, maybe under Mobile > Services > IGetSSID)

public interface IGetSSID
{
    string GetSSID();
}

Create the Android Implementation

[assembly: Dependency(typeof(GetSSIDAndroid))]
namespace yournamespace
{
    public class GetSSIDAndroid : IGetSSID
    {
        public string GetSSID()
        {
            WifiManager wifiManager = (WifiManager)(Android.App.Application.Context.GetSystemService(Context.WifiService));

            if (wifiManager != null && !string.IsNullOrEmpty(wifiManager.ConnectionInfo.SSID))
            {
                return wifiManager.ConnectionInfo.SSID;
            }
            else
            {
                return "WiFiManager is NULL";
            }
        }
    }
}

Then in your forms you get the SSID like this:

var ssid = DependencyService.Get<IGetSSID>().GetSSID();

Note: Don't forget to add this permission on your Android Manifest

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

Upvotes: 5

Related Questions