简体   繁体   English

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

[英]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. 我想检查用户是否通过wifi连接,即使没有互联网访问,也知道它连接到哪个Ssid。 I tried using WlanConnectionProfileDetails class but is getting some error. 我尝试使用WlanConnectionProfileDetails类,但出现了一些错误。 Any help would be appreciated.Thank You. 任何帮助将不胜感激。谢谢。

Its quite easy 这很容易

1.Declare this in your page 1.在您的页面中声明

private WiFiAdapter firstAdapter;
private string savedProfileName = null;

2.Populate it 2.填充

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 3.将此功能添加到您的应用清单文件中

    <DeviceCapability Name="wiFiControl" />

4.Check to which network your wifi is connected 4.检查您的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 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: 5,在这里保存的ProfileName将连接到网络ssid。如果通过wifi和connectedProfile连接,则connectedProfile.IsWlanConnectionProfile将为true。如果通过蜂窝网络连接,则IsWwanConnectionProfile将为true在此之后,您可以通过以下方法检查Internet:

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

Hope it helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中检查移动网络是否连接到互联网 - How to check if mobile network is connected to the internet or not in android 插入网络电缆/连接Wifi时通知 - Be informed when a network cable is plugged in / Wifi connected Windows 10移动UWP应用比wifi更喜欢蜂窝数据。 如何处理多个网络连接 - Windows 10 Mobile UWP app prefers cellular data over wifi. How to handle multiple network connections 检查是否仅连接到WiFi C# - Check if ONLY connected to WiFi C# 如何确定我是否在Windows Phone 8.1(通用应用程序)中连接到WiFi或移动网络? - How can I determine whether I am connected to WiFi or a mobile network in Windows Phone 8.1 (Universal app)? 如何在Windows IoT核心版/ UWP中获得连接到本地网络的设备的IP - How get device's ip those connected to local network in Windows IoT Core/UWP 如何通过bssid连接到wifi网络 - How to connect to wifi network by bssid 如何访问连接到平板电脑UWP的外部摄像头 - How to access external camera connected to a tablet UWP ShareLinkTask,检查用户是否已连接到网络 - ShareLinkTask, check if a user has connected to a network 是否可以检查谁连接到您的无线网络? - Is it possible to check who is connected to your wireless network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM