User can now detect the mobile operator and other network information's in Windows Phone 7.1 Mango devices programmatically. The new APIs to detect these information will help you to identify the mobile operator, availability of network, Wi-Fi access etc.

Let's jump into deep drive to know more about these APIs and the integration to your application using a small demo. Read to learn about it.

Index - Windows Phone 7 (Mango) Tutorial

Know more about the API

Let us first learn about the APIs present to detect the network information of the device programmatically. If you installed the Windows Phone 7.1 Beta Tools, you can use the DeviceNetworkInformation class to retrieve details about the network. It is a static class present inside the Microsoft.Phone.Net.NetworkInformation namespace.

It has the following properties exposed to user:

CellularMobileOperator Gets the name of the cellular mobile operator IsCellularDataEnabled Gets a value that indicates whether the network is cellular data enabled IsCellularDataRoamingEnabled Gets a value that indicates whether the network allows data roaming IsNetworkAvailable Gets a value that indicates whether the network is available IsWiFiEnabled Gets a value that indicates whether the network is Wi-Fi enabled

Remember that, these properties are readonly i.e. these exposes only a getter, you can't set any value there.

Demo: Exposing Properties to UI

Let's have a demo for this new feature. We will expose those properties to the UI and bind them to some text block controls. Here is the implementation of the code behind file of our MainPage.xaml screen:

/// <summary> /// Gets the name of the cellular mobile operator. /// </summary> public string Operator { get { return DeviceNetworkInformation.CellularMobileOperator; } } /// <summary> /// Gets a value that indicates whether the network is cellular data enabled. /// </summary> /// <value> /// <c>true</c> if this instance is cellular data enabled; otherwise, <c>false</c>. /// </value> public bool IsCellularDataEnabled { get { return DeviceNetworkInformation.IsCellularDataEnabled; } } /// <summary> /// Gets a value that indicates whether the network allows data roaming. /// </summary> /// <value> /// <c>true</c> if this instance is cellular data roaming enabled; otherwise, <c>false</c>. /// </value> public bool IsCellularDataRoamingEnabled { get { return DeviceNetworkInformation.IsCellularDataRoamingEnabled; } } /// <summary> /// Gets a value that indicates whether the network is available. /// </summary> /// <value> /// <c>true</c> if this instance hass network available; otherwise, <c>false</c>. /// </value> public bool IsNetworkAvailable { get { return DeviceNetworkInformation.IsNetworkAvailable; } } /// <summary> /// Gets a value that indicates whether the network is Wi-Fi enabled. /// </summary> /// <value> /// <c>true</c> if this instance has wi-fi enabled; otherwise, <c>false</c>. /// </value> public bool IsWiFiEnabled { get { return DeviceNetworkInformation.IsWiFiEnabled; } }

Demo: Creating the UI

Let's modify the ContentPanel to host a StackPanel which will consist of 5 TextBlock, each will be binded to the properties of the code behind class. Here is our XAML code, for your reference:

<!--ContentPanel - place additional content here--> < Grid x:Name ="ContentPanel" Grid . Row ="1" Margin ="12,0,12,0" > < StackPanel Orientation ="Vertical" Margin ="5" DataContext ="{Binding ElementName=phonePage}" > < TextBlock Text ="{Binding Operator, StringFormat='Operator: \{0\}'}" /> < TextBlock Text ="{Binding IsCellularDataEnabled, StringFormat='IsCellularDataEnabled: \{0\}'}" /> < TextBlock Text ="{Binding IsCellularDataRoamingEnabled, StringFormat='IsCellularDataRoamingEnabled: \{0\}'}" /> < TextBlock Text ="{Binding IsNetworkAvailable, StringFormat='IsNetworkAvailable: \{0\}'}" /> < TextBlock Text ="{Binding IsWiFiEnabled, StringFormat='IsWiFiEnabled: \{0\}'}" /> </ StackPanel > </ Grid >

Once done, we can run the application to see it in action. Let's build the project and once the build succeeded, run the application inside your phone emulator. You will see the following:

Here you will notice that, the operator name will show as "Fake GSM Network", because we are running the application inside the phone emulator. If you run the same inside original device, you will see the original operator name there.

The other properties will show you proper values. In phone emulator, you can't check all the properties but once you deploy them in your Windows Phone 7.1 device, you will see them properly mentioned in the screen.