Uwpbeginner Uwpbeginner - 24 days ago 27
C# Question

UWP-How to check if connected to a Wifi Network

I want to check if the user is connected over wifi even with no internet access and know to which Ssid it is connected.
I tried using WlanConnectionProfileDetails class  but is getting some error.
Any help would be appreciated.Thank You.

Answer

Its quite easy

1.Declare this in your page

private WiFiAdapter firstAdapter;

2.Populate it

var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
            if (result.Count >= 1)
            {
                firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id);
            }

3.Add this capability to your app manifest file

    <DeviceCapability Name="wiFiControl" />

4.Check to which network your wifi is connected

if (firstAdapter.NetworkAdapter.GetConnectedProfileAsync() != null)
            {
                var connectedProfile = await firstAdapter.NetworkAdapter.GetConnectedProfileAsync();
                if (connectedProfile != null && !connectedProfile.ProfileName.Equals(savedProfileName))
                {
                    savedProfileName = connectedProfile.ProfileName;
}

5.Here savedProfileName will have network ssid its connected.Also connectedProfile.IsWlanConnectionProfile will be true if connected over wifi and connectedProfile.IsWwanConnectionProfile will be true if connected over cellular After this you can check for internet by this method:

public static bool IsInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}

Hope it helped.

Comments