Windows Phone 7 (Mango) Tutorial - 15 - Detecting Device Information



In our last chapter of Windows Phone 7 Tutorial, we learnt about detecting network information using the static class called DeviceNetworkInformation. We also demonstrated it with a small demo application.

 

In this new chapter of the tutorial series, we will learn how to detect the device information using the APIs present inside the SDK. We will learn how to detect whether any Keyboard has been installed with the device, whether the device is running under battery or using any external power supply. We will also learn the way to detect about the memory of the device and other device information.

 

Read more to know about the class and it's properties with a small demonstration of the code.

 

Index - Windows Phone 7 (Mango) Tutorial

 

Know more about the API

Let's discuss about the API first, which we have to use to detect the information about the device. Using the class called "DeviceStatus" present inside the "Microsoft.Phone.Info" namespace we can detect more about the device. There are some properties exposed by this static class which only returns value that means, you can not set any value to them.

 

Here is the meta data of the class:

 
namespace Microsoft.Phone.Info
{
    public static class DeviceStatus
    {
        public static bool IsKeyboardDeployed { get; }
        public static bool IsKeyboardPresent { get; }
        public static PowerSource PowerSource { get; }
        public static long ApplicationCurrentMemoryUsage { get; }
        public static long ApplicationPeakMemoryUsage { get; }
        public static long DeviceTotalMemory { get; }
        public static string DeviceName { get; }
        public static string DeviceFirmwareVersion { get; }
        public static string DeviceHardwareVersion { get; }
        public static string DeviceManufacturer { get; }
        public static event EventHandler KeyboardDeployedChanged;
        public static event EventHandler PowerSourceChanged;
    }
}

 

 

The class implements the following properties. Let's describe each one of them here. The 10 properties exposed here describes the following:

 

IsKeyboardDeployed Indicates whether the user has deployed the physical hardware keyboard
IsKeyboardPresent Indicates whether the device contains a physical hardware keyboard
PowerSource Indicates if the device is currently running on battery power or external power
ApplicationCurrentMemoryUsage Returns the memory usage of the current application in bytes
ApplicationPeakMemoryUsage Returns the peak memory usage of the current application in bytes
DeviceTotalMemory Returns the physical RAM size of the device in bytes
DeviceName Returns the device name
DeviceFirmwareVersion Returns the firmware version running on the device
DeviceHardwareVersion Returns the hardware version running on the device
DeviceManufacturer Returns the device manufacturer name

 

Demo: Exposing Properties to UI

Now we will jump start to code to implement a small application to demonstrate the same. Open your MainPage.xaml and expose the same properties to the UI which will return the proper property value from DeviceStatus class. We will bind these properties to the UI later.

 

Here is the implementation of the same for your reference:

 
public bool IsKeyboardDeployed { get { return DeviceStatus.IsKeyboardDeployed; } }
public bool IsKeyboardPresent { get { return DeviceStatus.IsKeyboardPresent; } }
public string PowerSource { get { return DeviceStatus.PowerSource.ToString(); } }
public long ApplicationCurrentMemoryUsage { get { return DeviceStatus.ApplicationCurrentMemoryUsage; } }
public long ApplicationPeakMemoryUsage { get { return DeviceStatus.ApplicationPeakMemoryUsage; } }
public long DeviceTotalMemory { get { return DeviceStatus.DeviceTotalMemory; } }
public string DeviceName { get { return DeviceStatus.DeviceName; } }
public string DeviceFirmwareVersion { get { return DeviceStatus.DeviceFirmwareVersion; } }
public string DeviceHardwareVersion { get { return DeviceStatus.DeviceHardwareVersion; } }
public string DeviceManufacturer { get { return DeviceStatus.DeviceManufacturer; } }

 

Demo: Creating the UI

It's time to create the XAML page to demonstrate the example. We will add some TextBlock inside the ContentPanel of the MainPage.xaml and bind data to the respective properties present inside the code behind file.

 

Here is the implementation of the same. Have a look into the XAML code for details:

 

 
<!--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 IsKeyboardDeployed, StringFormat='IsKeyboardDeployed: \{0\}'}"/>
        <TextBlock Text="{Binding IsKeyboardPresent, StringFormat='IsKeyboardPresent: \{0\}'}"/>
        <TextBlock Text="{Binding PowerSource, StringFormat='PowerSource: \{0\}'}"/>
        <TextBlock Text="{Binding ApplicationCurrentMemoryUsage, StringFormat='CurrentMemoryUsage: \{0\}'}"/>
        <TextBlock Text="{Binding ApplicationPeakMemoryUsage, StringFormat='PeakMemoryUsage: \{0\}'}"/>
        <TextBlock Text="{Binding DeviceTotalMemory, StringFormat='TotalMemory: \{0\}'}"/>
        <TextBlock Text="{Binding DeviceName, StringFormat='DeviceName: \{0\}'}"/>
        <TextBlock Text="{Binding DeviceFirmwareVersion, StringFormat='DeviceFirmwareVersion: \{0\}'}"/>
        <TextBlock Text="{Binding DeviceHardwareVersion, StringFormat='DeviceHardwareVersion: \{0\}'}"/>
        <TextBlock Text="{Binding DeviceManufacturer, StringFormat='DeviceManufacturer: \{0\}'}"/>
    </StackPanel>
</Grid>

 

 

Once your XAML code is ready, run the project after building the source code successfully. It will launch the phone emulator on the screen. Our application will launch shortly. Once loaded in the screen, you will find the following information inside the content panel:

 

Device Status Demo using Windows Phone 7.1 (Mango) Emulator

 

As we did data binding to our UI, it will load proper value with the help of the Device Status class. The API will return the value based on the current device information. Because, we are running it inside the emulator, we will see some different value than the original device. If you run it inside the original device, you will see proper value for the versions and other fields.

 

Hope this chapter of the tutorial series is also helpful to you. Do provide your feedback at the end of the page. Stay tuned for the next chapters of the tutorial series. Don't forget to like my Facebook Fan Page for latest update on the articles.

2 comments

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

    ReplyDelete
  2. Check your XAML and bindings. Did you modify your XAML page with all the fields? Also check whether your changed the opacity or visibility of the panel where you hosted the text.

    ReplyDelete


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