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

7

我目前正在将我的一个库移植到Windows Phone 8.1 Runtime,并遇到了一个在Windows Phone 8.0和Windows Phone Silverlight 8.1应用程序中都可以使用的缺失API。

我需要的是DeviceNetworkInformation,以获取设备连接到互联网的网络接口类型。

以下是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上可用MSDN - BironDavid
1
似乎命名空间在Windows Phone 8.1 Runtime应用程序中不可用,但在Silverlight 8.1中可用。 - George Taskos
3个回答

9

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

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

以下方法获取连接类型,就像在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;

1
如果你对网络的技术信息更感兴趣,我找到的最好资料是关于 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 类中,您有像 IanaInterfaceTypeInboundMaxBitsPerSecondOutboundMaxBitsPerSecond 这样的属性,它们可以让您了解网络速度有多快。例如,IEEE 802.11 Wifi 的 IanaInterfaceType 是 71...您可以在这里查看更多详细信息: NetworkAdapterIanaInterfaceType
编辑:这里是完整的 Iana 接口类型 列表。

1
这段文字讲述了如何在Windows 8.1中检测网络类型。虽然与早期版本不完全相同,但我们仍可以做类似的事情,只是命名空间和类有所不同。实际上,您可以根据NetworkCostType调整行为,而不是访问网络连接的详细信息。如果用户的连接没有计量,您可以随意操作,但如果他们使用的是数据计划或者会产生费用的网络,您应该提示用户或者采取不同的处理方式,例如等待WiFi或以太网连接。这篇好文章涵盖了大部分可能出现的情况。

嗨,Liam,谢谢你的回答,我会查看信息并尽快回复。 - George Taskos
好的,我从两个答案中得到了想法,但我觉得废弃具有合理方法和属性的Microsoft.Phone命名空间非常愚蠢。你有什么办法获取运营商信息吗?我似乎找不到它。 - George Taskos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接