Windows Phone 7 (Mango) Tutorial - 14 - Detecting Network Information of the Device


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:

 

Device Network Information Demo using Windows Phone 7.1 (Mango) Emulator

 

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.


9 comments

  1. Hello Kunal!

    I'd like to ask you if you know ANY way of detecting nearby wifi networks and their signal strengths without having to be connected to them already?

    I'm trying to create a wifi-triangulation application but can't "reach out" to any networks.

    ReplyDelete
  2. Hi Viktor, I am not aware of it. You may ask in the forums to get answered about this query. Let me know, if I can help you in any case.

    ReplyDelete
  3. hello kunal i tried to run the app but a blank screen is being displayed any idea?

    ReplyDelete
  4. Hi, could you explain how to implement the 'code behind file'? Where should the code above be copied to? Is it mainpage.xaml.cs?

    ReplyDelete
  5. You can put the code anywhere from where you want to call/execute it.

    ReplyDelete
  6. hello kunal i tried to run the app but a blank screen is being displayed any idea?

    ReplyDelete
    Replies
    1. You might have implemented a blank screen only. Also check whether you have the proper page set as default. If all are fine, share me the solution. I will check out the issue.

      Delete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top