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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • It's hardly surprising that half of small businesses fail within the first 1-5 years. It's not easy to launch a new product, single-handedly manage everything from IT to accounting, fend off the competition, and grow a customer base – all at the same time – even with a great concept. Offering awesome customer service can make the difference between a startup that flies and a startup that dies. Read this white paper to learn nine ways customer support can help you beat the competition and grow your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds