How to get connection and carrier information in Windows Phone 8.1 Runtime
c#, windows-phone-8, windows-phone-8.1
Solution
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;
Problem
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`.