2016-11-09 179 views
0

我想检查用户是否通过wifi连接,即使没有互联网接入,并知道它连接到哪个Ssid。 我尝试使用WlanConnectionProfileDetails类,但得到一些错误。 任何帮助,将不胜感激。谢谢。UWP-如何检查是否连接到Wifi网络

回答

1

它很容易

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

<DeviceCapability Name="wiFiControl" /> 

4.检查w HICH网络您的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,如果连接通过蜂窝 之后 connectedProfile.IsWwanConnectionProfile将是真正的connected.Also connectedProfile.IsWlanConnectionProfile为真你可以通过这种方法检查互联网:

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

希望它有帮助。

+0

非常感谢哥们,它为我工作。 – Uwpbeginner