系统相关
首页 > 系统相关> > Windows 10 UWP – C#:如何检查网络类型(EDGE / 3G / LTE),不仅仅是蜂窝网络与WLAN?

Windows 10 UWP – C#:如何检查网络类型(EDGE / 3G / LTE),不仅仅是蜂窝网络与WLAN?

作者:互联网

如何查看目前使用的蜂窝连接类型(EDGE / 3G / LTE)?我只知道如何检查设备是否在蜂窝连接或WLAN上,但我需要特定类型的蜂窝连接.谢谢!

解决方法:

NetworkInformation应该适用于Windows 10(https://msdn.microsoft.com/en-us/library/windows.networking.connectivity.networkinformation.getinternetconnectionprofile.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2).

您必须根据需要调整此代码:

    /// <summary>
    ///  ("Connection Type", `0`-`3`): `0` - cellular, `1` - wifi / ethernet, `2` - inne;
    /// </summary>
    /// <returns></returns>
    public byte GetConnectionGeneration()
    {
        try
        {
            ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
            if (profile.IsWwanConnectionProfile)
            {
                WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
                switch (connectionClass)
                {
                    //2G-equivalent
                    case WwanDataClass.Edge:
                    case WwanDataClass.Gprs:
                    //3G-equivalent
                    case WwanDataClass.Cdma1xEvdo:
                    case WwanDataClass.Cdma1xEvdoRevA:
                    case WwanDataClass.Cdma1xEvdoRevB:
                    case WwanDataClass.Cdma1xEvdv:
                    case WwanDataClass.Cdma1xRtt:
                    case WwanDataClass.Cdma3xRtt:
                    case WwanDataClass.CdmaUmb:
                    case WwanDataClass.Umts:
                    case WwanDataClass.Hsdpa:
                    case WwanDataClass.Hsupa:
                    //4G-equivalent
                    case WwanDataClass.LteAdvanced:
                        return 0;

                    //not connected
                    case WwanDataClass.None:
                        return 2;

                    //unknown
                    case WwanDataClass.Custom:
                    default:
                        return 2;
                }
            }
            else if (profile.IsWlanConnectionProfile)
            {
                return 1;
            }
            return 2;
        }
        catch (Exception)
        {
            return 2; //as default
        }

    }

标签:c,windows,windows-10,windows-10-mobile
来源: https://codeday.me/bug/20190528/1168639.html