简体   繁体   English

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

[英]How to get connection and carrier information in Windows Phone 8.1 Runtime

I currently am porting one of my libraries to Windows Phone 8.1 Runtime and stepped into a missing API which you can use in Windows Phone 8.0 and Windows Phone Silverlight 8.1 apps. 我目前正在将我的一个库移植到Windows Phone 8.1 Runtime并进入一个缺少的API,您可以在Windows Phone 8.0和Windows Phone Silverlight 8.1应用程序中使用它。

What I need is the DeviceNetworkInformation to get with what kind of NetworkInterfaceType is the device connected to the internet. 我需要的是DeviceNetworkInformation来获取连接到互联网的设备是什么类型的NetworkInterfaceType。

Sample code in Windows Phone 8.0. 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);
}

And you could access the carrier's name with DeviceNetworkInformation.CellularMobileOperator . 您可以使用DeviceNetworkInformation.CellularMobileOperator访问运营商的名称。

EDIT: The following suggestion works Windows Phone 8.1 Apps. 编辑:以下建议适用于Windows Phone 8.1应用程序。

I would not recommend using IanaInterfaceType , InboundMaxBitsPerSecond or OutboundMaxBitsPerSecond to determine the connection type, as they are very inaccurate. 我不建议使用IanaInterfaceTypeInboundMaxBitsPerSecondOutboundMaxBitsPerSecond来确定连接类型,因为它们非常不准确。

The following method gets the connection type as seen in the status bar of the WP. 以下方法获取WP中状态栏中显示的连接类型。 Note that the mode of connection does not necessarily give an indication of the upload / download speed! 请注意,连接模式不一定表示上传/下载速度!

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;
}

Sorry, I don't know about the carrier's name, but the Access Point Name (APN) might also be of use, as the Access Point is connected to the carrier: 抱歉,我不知道运营商的名称,但接入点名称(APN)也可能有用,因为接入点已连接到运营商:

ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
string apn = profile.WwanConnectionProfileDetails.AccessPointName;

It is not all exactly the same but we can do pretty much the same in Windows 8.1 for detecting network type as we could on earlier versions, except the namespace and classes are different. 它并非完全相同,但我们在Windows 8.1中可以像在早期版本中那样检测网络类型,但命名空间和类不同。 A good article that covers most of the possible scenarios can be found here. 可以在此处找到涵盖大多数可能方案的好文章。

Effectively instead of accessing the specifics about the network connection you adjust your behavior based on the NetworkCostType . 有效地,而不是访问有关网络连接的细节,您可以根据NetworkCostType调整您的行为。 Basically if the user's connection is one that has little to no metering you do what you like, but if they are on a data plan or at a place where making a call out to the Internet will incur a fee to the user you should prompt them or handle it differently. 基本上,如果用户的连接是几乎没有计量的连接,那么你可以做你喜欢的事情,但是如果他们在数据计划上,或者在拨打互联网的地方向用户收取费用,你应该提示他们或以不同方式处理。 Perhaps waiting until Wifi or ethernet is available. 也许等到Wifi或以太网可用。

If you are interested more in the technical information about the network, the best I could find are the properties of the ConnectionProfile.NetworkAdapter class. 如果您对网络的技术信息更感兴趣,我能找到的最好的是ConnectionProfile.NetworkAdapter类的属性。 You can get instance of ConnectionProfile class like this (taken from this Microsoft article ): 您可以像这样获取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());
    }

In NetworkAdapter class you have properties like IanaInterfaceType , InboundMaxBitsPerSecond and OutboundMaxBitsPerSecond which give you pretty good idea about how fast your network is. NetworkAdapter类中,您具有IanaInterfaceTypeInboundMaxBitsPerSecondOutboundMaxBitsPerSecondIanaInterfaceType ,可以让您非常了解网络的速度。 For example, IanaInterfaceType for IEEE 802.11 Wifi is 71...You can look at more details here: NetworkAdapter and IanaInterfaceType . 例如,IEEE 802.11 Wifi的IanaInterfaceType为71 ...您可以在此处查看更多详细信息: NetworkAdapterIanaInterfaceType

Edit: Here is the complete list of Iana interface types 编辑:以下是Iana界面类型的完整列表

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

相关问题 Windows Phone 8.1运行时-如何获取呼叫信息,例如来电和去电的历史记录? - Windows Phone 8.1 Runtime - How to get call information such as the history of incoming and outgoing calls? 在Windows Phone Runtime 8.1中获取url参数 - Get url parameters in Windows Phone Runtime 8.1 如何显示Windows Phone 8.1的传递信息 - How to display passed information for Windows Phone 8.1 如何在Windows Phone 8 SL中获取设备运营商和国家/地区 - How to get device carrier and country within Windows Phone 8 SL Windows Phone 8.1运行时应用 - Windows phone 8.1 runtime apps Windows Phone 8.1运行时应用程序无法获取TextBlock ActualWidth值 - cannot get TextBlock ActualWidth value in windows phone 8.1 runtime app 如何将项目从 Windows Phone 8.1 Silverlight 转换为 Windows Phone 8.1 Runtime - How to convert project from Windows Phone 8.1 Silverlight to Windows Phone 8.1 Runtime Windows Phone 8.1 Sql连接 - Windows Phone 8.1 Sql connection 如何开始使用Windows Phone SDK 8.1? - How to get started with windows phone SDK 8.1? 如何在Windows Phone 8.1运行时应用程序中显示计划的对话框? - How to show a scheduled dialog in a Windows Phone 8.1 Runtime app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM