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

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming webcast to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds