c# - Windows 10 UWP - C# : How to check network type (EDGE/3G/LTE), 不仅是蜂窝还是 WLAN?

标签 c# windows windows-10 windows-10-mobile

<分区>

如何检查当前使用的是哪种蜂窝网络连接(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 10 UWP - C# : How to check network type (EDGE/3G/LTE), 不仅是蜂窝还是 WLAN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33539251/

相关文章:

c# - 应该使用方法参数还是通过引用将命令信息传递给聚合?

c++ - Chromium 浏览器在 module_list.cc : Check Failed 中构建 fatal error

c# - Nuget 3.2.0 不在项目中添加额外的文件/文件夹

windows - 如何仅使用 Windows 命令 shell 查找文件大小?

c# - 请求身份验证 token 的应用程序被禁用或配置不正确

在 wsl VERR_PATH_NOT_FOUND 上使用 virtualbox Vagrant

c# - 不使用分发服务器的 NServiceBus F5 负载均衡订阅者

c# - 为什么我最后提到的组会捕获其他所有组?

c# - 在这种情况下可以覆盖 ToString() 方法吗

ios - 使用 iFunbox、iTunes 在 iphone 上安装 IPA 文件