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
.
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;
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With