c# – 如何在Windows Phone 8.1 Runtime中获取连接和操作符信息

前端之家收集整理的这篇文章主要介绍了c# – 如何在Windows Phone 8.1 Runtime中获取连接和操作符信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在将我的一个库移植到 Windows Phone 8.1 Runtime并进入一个缺少的API,您可以在Windows Phone 8.0和Windows Phone Silverlight 8.1应用程序中使用它.

我需要的是DeviceNetworkInformation来获取连接到互联网的设备是什么类型的NetworkInterfaceType.

Windows Phone 8.0中的示例代码.

public void GetDeviceConnectionInfo()
{
    DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint("microsoft.com",80),nrr =>
        {
            NetworkInterfaceInfo info = nrr.NetworkInterface;
            if (info != null)
            {
                switch (info.InterfaceType)
                {
                    case NetworkInterfaceType.Ethernet:
                        // Do something
                        break;
                    case NetworkInterfaceType.MobileBroadbandCdma:
                    case NetworkInterfaceType.MobileBroadbandGsm:
                        switch (info.InterfaceSubtype)
                        {
                            case NetworkInterfaceSubType.Cellular_3G:
                            case NetworkInterfaceSubType.Cellular_EVDO:
                            case NetworkInterfaceSubType.Cellular_EVDV:
                            case NetworkInterfaceSubType.Cellular_HSPA:
                                // Do something
                                break;
                        }
                        // Do something
                        break;
                    case NetworkInterfaceType.Wireless80211:
                        // Do something
                        break;
                }
            }
        },null);
}

您可以使用DeviceNetworkInformation.CellularMobileOperator访问操作符的名称.

解决方法

编辑:以下建议适用于Windows Phone 8.1应用程序.

我不建议使用IanaInterfaceType,InboundMaxBitsPerSecond或OutboundMaxBitsPerSecond来确定连接类型,因为它们非常不准确.

以下方法获取WP中状态栏中显示的连接类型.请注意,连接模式不一定表示上传/下载速度!

using Windows.Networking.Connectivity;

/// <summary>
/// Detect the current connection type
/// </summary>
/// <returns>
/// 2 for 2G,3 for 3G,4 for 4G
/// 100 for WiFi
/// 0 for unknown or not connected</returns>
private static byte GetConnectionGeneration()
{
    ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
    if (profile.IsWwanConnectionProfile)
    {
        WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
        switch (connectionClass)
        {
            //2G-equivalent
            case WwanDataClass.Edge:
            case WwanDataClass.Gprs:
                return 2;

            //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:
                return 3;

            //4G-equivalent
            case WwanDataClass.LteAdvanced:
                return 4;

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

            //unknown
            case WwanDataClass.Custom:
            default:
                return 0;
        }
    }
    else if (profile.IsWlanConnectionProfile)
    {
        return 100;
    }
    return 0;
}

抱歉,我不知道操作符的名称,但接入点名称(APN)也可能有用,因为接入点已连接到操作符:

ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
string apn = profile.WwanConnectionProfileDetails.AccessPointName;
原文链接:https://www.f2er.com/csharp/100925.html

猜你在找的C#相关文章