Friday, May 27, 2011

Mango : DeviceNetworkInformation in Windows Phone 7.1

Hope you already downloaded Windows Phone 7.1 Mango Beta Tools as I mentioned in my last post here

Today I am going to talk about DeviceNetworkInformation API with small demo. This API is exposed with the new toolset and very useful especially when you need to check various things related to Mobile Operator. Let’s have a look in more depth about this API.

What this API does ? :

This API helps you to identify and know more about Network capabilities by which one can identify Availability of Network, Availability of Roaming Data and WiFi Network etc. How to make use of this in your Windows Phone 7.1 app its totally your call and it is dependent on your Application Requirement. So I will leave this “Where to use” part to you.

Structure of DeviceNetworkInformation API :

DeviceNetworkInformation is a static class insider Microsoft.Phone.Net.NetworkInformation namespace having few boolean properties and ResolveHostNameAsync method and NetworkAvailabilityChanged. Since all the properties are static you don’t need to create instance. Properties are

  • CellularMobileOperator
  • IsCellularDataEnabled
  • IsCellularDataRoamingEnabled
  • IsNetworkAvailable
  • IsWiFiEnabled

It resides in Microsoft.Phone.dll(v2.0.50727) which you can find in WindowsPhone71 directory.

General Structure is like this :

namespace Microsoft.Phone.Net.NetworkInformation
    public static class DeviceNetworkInformation
        public static string CellularMobileOperator { get; }
        public static bool IsCellularDataEnabled { get; }    
        public static bool IsCellularDataRoamingEnabled { get; }     
        public static bool IsNetworkAvailable { get; }      
        public static bool IsWiFiEnabled { get; }

        public static event EventHandler<NetworkNotificationEventArgs> NetworkAvailabilityChanged;

        public static void ResolveHostNameAsync(DnsEndPoint endPoint, NameResolutionCallback callback, object context);
        public static void ResolveHostNameAsync(DnsEndPoint endPoint, NetworkInterfaceInfo networkInterface, NameResolutionCallback callback, object context);
Now Let’s build one small App on top of this API, So here are steps :

How it will look like :


*Please note that Images used in this post are from Internet found during regular search, Images have their respective copyrights and they are part of this post just for demo purpose.


<!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Explore .NET With Vikram Pendse" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Network Info" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

<!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Image Source="GSMNetwork.jpg" Opacity="0.3" Height="607" HorizontalAlignment="Left" Margin="-2,-6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="458" />
            <TextBlock FontWeight="Bold" x:Name="txtMobileOptr" Height="30" HorizontalAlignment="Left" Margin="0,104,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtNetworkAvail" Height="30" HorizontalAlignment="Left" Margin="0,158,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtCDEnable" Height="30" HorizontalAlignment="Left" Margin="0,212,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtRoaming" Height="30" HorizontalAlignment="Left" Margin="2,269,0,0" VerticalAlignment="Top" Width="448" />
            <TextBlock FontWeight="Bold" x:Name="txtWiFi" Height="56" TextWrapping="Wrap" HorizontalAlignment="Left" Margin="0,325,0,0" VerticalAlignment="Top" Width="450" />
            <Image x:Name="imgWiFi" Height="150" HorizontalAlignment="Left" Margin="228,431,0,0" Stretch="Fill" VerticalAlignment="Top" Width="200" />

So on designer layout in Visual Studio it will look like this :


C# Code :

void MainPage_Loaded(object sender, RoutedEventArgs e)

public void GetDeviceNetworkInformation()
     txtMobileOptr.Text = "Your Mobile Operator is : " + DeviceNetworkInformation.CellularMobileOperator.ToString();
     if (DeviceNetworkInformation.IsNetworkAvailable)
        txtNetworkAvail.Text = "Network is Available";
        txtNetworkAvail.Text = "Network is Not Available";

// Similarly you can get and set other properties to Textblocks


Output :



Now if you need to make it more Jazzy you can put icons and make it more attractive and friendly. For example, I have done for Wi Fi, I am showing Wi Fi enable – disable by displaying PNG file like this :

NetworkWiFiEnable          NetworkWiFiDisable

I know the images used here are bit dull,but idea is to show how you can enhance the app by such ideas and code for this is very simple like this :

if (DeviceNetworkInformation.IsWiFiEnabled)
    txtWiFi.Text = "You are connected to WiFi Network";
    Uri uri = new Uri(@"wificon.png", UriKind.Relative);
    ImageSource imgSource = new BitmapImage(uri);
    imgWiFi.Source = imgSource;
    txtWiFi.Text = "You are not connected to WiFi Network,Check your WiFi Settings and Retry";
    Uri uri = new Uri(@"wifidiscon.png", UriKind.Relative);
    ImageSource imgSource = new BitmapImage(uri);
    imgWiFi.Source = imgSource;

So this is how you can make use of DeviceNetworkInformation in your Windows Phone 7.1 Application, As I mentioned earlier that how you will use this is totally your choice and depends on business scenario, But next time when you need all Network related information inside your app, you don’t need to search, The solution is already with you with this API. See you then with more good stuff on Mango aka Windows Phone 7.1 in coming posts.Keep buzzing over emails and in comment section as usual.


1 comment:

Irfan said...

great info, but IMHO you could just wrap the code with tools like to get nicer looks :)