繁体   English   中英

如何在Windows Phone 8.1 Runtime中获取连接和运营商信息

[英]How to get connection and carrier information in 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应用程序。

我不建议使用IanaInterfaceTypeInboundMaxBitsPerSecondOutboundMaxBitsPerSecond来确定连接类型,因为它们非常不准确。

以下方法获取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;

它并非完全相同,但我们在Windows 8.1中可以像在早期版本中那样检测网络类型,但命名空间和类不同。 可以在此处找到涵盖大多数可能方案的好文章。

有效地,而不是访问有关网络连接的细节,您可以根据NetworkCostType调整您的行为。 基本上,如果用户的连接是几乎没有计量的连接,那么你可以做你喜欢的事情,但是如果他们在数据计划上,或者在拨打互联网的地方向用户收取费用,你应该提示他们或以不同方式处理。 也许等到Wifi或以太网可用。

如果您对网络的技术信息更感兴趣,我能找到的最好的是ConnectionProfile.NetworkAdapter类的属性。 您可以像这样获取ConnectionProfile类的实例(取自本Microsoft文章 ):

    //Get the Internet connection profile
    string connectionProfileInfo = string.Empty;
    try {
        ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

        if (InternetConnectionProfile == null) {
            NotifyUser("Not connected to Internet\n");
        }
        else {
            connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
            NotifyUser("Internet connection profile = " +connectionProfileInfo);
        }
    }
    catch (Exception ex) {
        NotifyUser("Unexpected exception occurred: " + ex.ToString());
    }

NetworkAdapter类中,您具有IanaInterfaceTypeInboundMaxBitsPerSecondOutboundMaxBitsPerSecondIanaInterfaceType ,可以让您非常了解网络的速度。 例如,IEEE 802.11 Wifi的IanaInterfaceType为71 ...您可以在此处查看更多详细信息: NetworkAdapterIanaInterfaceType

编辑:以下是Iana界面类型的完整列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM