Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

What I need is the DeviceNetworkInformation to get with what kind of NetworkInterfaceType is the device connected to the internet.

Sample code in 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.

like image 837
George Taskos Avatar asked Jun 02 '14 21:06

George Taskos


3 Answers

EDIT: The following suggestion works Windows Phone 8.1 Apps.

I would not recommend using IanaInterfaceType, InboundMaxBitsPerSecond or OutboundMaxBitsPerSecond to determine the connection type, as they are very inaccurate.

The following method gets the connection type as seen in the status bar of the 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:

ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
string apn = profile.WwanConnectionProfileDetails.AccessPointName;
like image 177
Florian-Rh Avatar answered Nov 07 '22 11:11

Florian-Rh


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. 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. 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.

like image 1
Liam Avatar answered Nov 07 '22 11:11

Liam


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. You can get instance of ConnectionProfile class like this (taken from this Microsoft article):

    //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. For example, IanaInterfaceType for IEEE 802.11 Wifi is 71...You can look at more details here: NetworkAdapter and IanaInterfaceType.

Edit: Here is the complete list of Iana interface types

like image 1
stambikk Avatar answered Nov 07 '22 11:11

stambikk