Getting Network Information for your Windows Phone Device

Introduction

Knowing the nature of an internet connection is important for application developers. People usually have limits on the amount of data permitted as part of the data plan and application developers should be respectful of limiting data their application consumes. However, when the application is connected by Ethernet, such limits might no longer be applicable. Having knowledge of how the device is connected can be used to make some important decisions on what data cap the device needs to adhere to.

Windows Phone Networking Basics

Windows Phone platform provides robust support for getting the network information of the device.

The Microsoft.Phone.Net.NetworkInformation namespace provides a list of classes to support getting this information.

The DeviceNetworkInformation class has properties that can retrieve the name of the cellular mobile operator. The class also has properties that determine whether a wifi connection exists.

Hands-On

Let's create an application that demonstrates how this information can be used.

Create a new Visual Studio project called WpNetworkDemo.

New Visual Studio project
New Visual Studio project

When prompted, choose WP 7.1 as the target OS.

WP 7.1 as the target OS
WP 7.1 as the target OS

Next, add a textbox and 3 checkboxes that will contain the information we retrieve from the network.

Next, open MainPage.xaml.cs and include the Microsoft.Phone.Net.NetworkInformation namespace in the "using"  declarations.

using Microsoft.Phone.Net.NetworkInformation;

Next, add an instance of NetworkInterfaceType class as a property of MainPage.xaml class.

    public partial class MainPage : PhoneApplicationPage
    {
        NetworkInterfaceType networkInterfaceType;
        // Constructor
        public MainPage()

In the constructor, wire up the event handler to listen to the NetworkAvailabilityChanged Event on the DeviceNetworkInformation class. Additionally, add a placeholder call to a to-be-implemented method "LoadInitialInformation() in the constructor. We will deal with this method later.

public MainPage()
        {
            InitializeComponent();
            DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(DeviceNetworkInformation_NetworkAvailabilityChanged);
            LoadInitialInformation();
        }

Now, we implement the LoadInitialInformation() method. In this method, we will retrieve the network information type and set the value of the textbox. We will also retrieve the value about whether the device is connected over wifi, whether the Data over Cellular network is enabled and whether Data Roaming is enabled over cellular network.

We use this information to populate the UI controls, which show the state of the network connection.

public void LoadInitialInformation()
        {
 
            networkInterfaceType = NetworkInterface.NetworkInterfaceType;
            textBoxNetworkInterfaceType.Text = networkInterfaceType.ToString();
            checkBoxWifi.IsChecked = DeviceNetworkInformation.IsWiFiEnabled;
            checkBoxDataRoaming.IsChecked = DeviceNetworkInformation.IsCellularDataRoamingEnabled;
            checkBoxDataEnabled.IsChecked = DeviceNetworkInformation.IsCellularDataEnabled;
        }

Finally, we implement the event handler for the NetworkAvailabilityChanged event. Here, we will simply make a call to LoadInitialInformation(), which will refresh the information shown to the user.

void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
        {
            LoadInitialInformation();
        }

Optionally, you also want to add a button that can refresh the network state on demand. You can use this button to reset the information if you accidentally clicked on a control and changed its value.

private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            LoadInitialInformation();
        }

We are now ready to build and test our sample. If you are having trouble following along, you can get the copy of the sample code below.

When we run the application, you will notice the values are updated when the application is loaded.

Run the application
Run the application

If you run from the emulator, you will only Get Wireless80211 as the network interface. You need to run this application on a real physical device and change the wifi connection to see the updates to the data shown on the screen.

Summary

In this article, we learned how to get the network information on Windows Phone devices. I hope you have found this information useful.

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul_d_patel@hotmail.com



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds