微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/100925.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐