簡體   English   中英

UWP-如何檢查是否連接到Wifi網絡

[英]UWP-How to check if connected to a Wifi Network

我想檢查用戶是否通過wifi連接,即使沒有互聯網訪問,也知道它連接到哪個Ssid。 我嘗試使用WlanConnectionProfileDetails類,但出現了一些錯誤。 任何幫助將不勝感激。謝謝。

這很容易

1.在您的頁面中聲明

private WiFiAdapter firstAdapter;
private string savedProfileName = null;

2.填充

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.檢查您的WiFi連接到哪個網絡

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

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;
}

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM