[英]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.