UWP-如何检查是否连接到 Wifi 网络

UWP-How to check if connected to a Wifi Network

我想检查用户是否通过 wifi 连接,即使没有互联网访问,并且知道它连接到哪个 Ssid。 我尝试使用 WlanConnectionProfileDetails class 但遇到了一些错误。 任何帮助都将是 appreciated.Thank 你。

很简单

1.Declare 这在你的页面中

private WiFiAdapter firstAdapter;
private string savedProfileName = null;

2.Populate这

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

3.Add 此功能添加到您的应用程序清单文件

    <DeviceCapability Name="wiFiControl" />

4.Check你的wifi连接到哪个网络

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

5.Here savedProfileName 将具有网络 ssid,如果通过 wifi 连接,其 connected.Also connectedProfile.IsWlanConnectionProfile 将为真,并且 connectedProfile.IsWwanConnectionProfile 如果通过蜂窝网络连接则为真 在此之后,您可以通过以下方法检查互联网:

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

希望对您有所帮助。