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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds