Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 22 – Working with Device Information in WP7

 

Introduction:

In this article we are going to see how to get the Device information for managing the device. We will see how to make use of the Device detection API’s available with the Windows Phone 7 SDK to detect the Device information, Memory detection, Keyboard installed or not etc with the API. 

To detect the device information, we are going to use the DeviceExtendedProperties class. We have different options to get the information of the device, network etc.. with the API. We will see only retrieving the device information here with this article.  [more]

We are going to retrieve the below list of device information

    S No

Information

Description

1

DeviceUniqueId

Gets device Unique ID.

2

DeviceManufacturer

Gets Device Manufacture details.

3

ApplicationCurrentMemoryUsage

Gets Application Memory Usage.

4

ApplicationPeakMemoryUsage

Gets Application Memory Leak Usage.

5

DeviceName

Gets the Device Name.

6

DeviceFirmwareVersion

Gets the Firmware Version.

7

DeviceHardwareVersion

Gets the Hardware Version.

8

DeviceTotalMemory

Gets the physical Memory RAM Usage.

9

Power Source

Indicates if the device works with power or battery.

10

Iskeyboardpresent

Indicates if any physical keyboard is installed with device

11

IsKeyboardDeployed

Indicates if the user deploy the physical keyboard

12

ApplicationMemoryUsageLimit

Gets the maximum amount of memory.

Let us jump start to see the step by step process on how to implement the device information class to get the information and show it to the end users.

Steps:

Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 Application with a valid project name as shown in the screen below.

clip_image002
Now let us start with our design to see the device informations in one page. Once we are done with the design we can see the screen looks like below screen. Just copy the XAML code to get the same look and feel. Else we can directly drag and drop the controls from the tool box and design it as per the requirement.

XAML Code:

01

<phone:PhoneApplicationPage

02

x:Class="F5debugWp7DeviceInformation.MainPage"

03

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

04

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

05

xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

06

xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

07

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

08

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

09

mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

10

FontFamily="{StaticResource PhoneFontFamilyNormal}"

11

FontSize="{StaticResource PhoneFontSizeNormal}"

12

Foreground="{StaticResource PhoneForegroundBrush}"

13

SupportedOrientations="Portrait" Orientation="Portrait"

14

shell:SystemTray.IsVisible="True">

15

 

16

<!--LayoutRoot is the root grid where all page content is placed-->

17

<Grid x:Name="LayoutRoot" Background="Transparent">

18

<Grid.RowDefinitions>

19

<RowDefinition Height="Auto"/>

20

<RowDefinition Height="*"/>

21

</Grid.RowDefinitions>

22

 

23

<!--TitlePanel contains the name of the application and page title-->

24

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

25

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

26

<TextBlock x:Name="PageTitle" Text="Device Info" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

27

</StackPanel>

28

 

29

<!--ContentPanel - place additional content here-->

30

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

31

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,23,0,0" Name="ttDeviceInfo" Text="F5debug - Device Information List" VerticalAlignment="Top" Width="438" />

32

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,76,0,0" Name="ttdeviceuniqueid" Text="Device Unique ID:" VerticalAlignment="Top" Width="176" />

33

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,76,0,0" Name="txtDeviceUniqueID" Text="" VerticalAlignment="Top" Width="219" />

34

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,112,0,0" Name="textBlock4" Text="Device Manufacturer:" VerticalAlignment="Top" Width="200" />

35

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,112,0,0" Name="txtDeviceManufacturer" Text="" VerticalAlignment="Top" Width="219" />

36

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,148,0,0" Name="textBlock6" Text="Device Name:" VerticalAlignment="Top" Width="200" />

37

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,148,0,0" Name="txtDeviceName" Text="" VerticalAlignment="Top" Width="219" />

38

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,184,0,0" Name="textBlock8" Text="Firmware Version:" VerticalAlignment="Top" Width="200" />

39

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,184,0,0" Name="txtFirmwareVersion" Text="" VerticalAlignment="Top" Width="219" />

40

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,220,0,0" Name="textBlock10" Text="Hardware Version:" VerticalAlignment="Top" Width="200" />

41

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,220,0,0" Name="txtHardwareVersion" Text="" VerticalAlignment="Top" Width="219" />

42

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,251,0,0" Name="textBlock12" Text="Total Memory:" VerticalAlignment="Top" Width="200" />

43

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,251,0,0" Name="txtTotalMemory" Text="" VerticalAlignment="Top" Width="219" />

44

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,287,0,0" Name="textBlock14" Text="Current Memory:" VerticalAlignment="Top" Width="200" />

45

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,287,0,0" Name="txtCurrentMemory" Text="" VerticalAlignment="Top" Width="219" />

46

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,323,0,0" Name="textBlock16" Text="Peak Memory:" VerticalAlignment="Top" Width="200" />

47

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,323,0,0" Name="txtPeakMemory" Text="" VerticalAlignment="Top" Width="219" />

48

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,394,0,0" Name="textBlock18" Text="Power Source:" VerticalAlignment="Top" Width="200" />

49

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,394,0,0" Name="txtPowerSource" Text="" VerticalAlignment="Top" Width="219" />

50

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,430,0,0" Name="textBlock20" Text="Keyboard Installed:" VerticalAlignment="Top" Width="200" />

51

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,430,0,0" Name="txtkeyboardinstalled" Text="" VerticalAlignment="Top" Width="219" />

52

<Button Content="Get Details" Height="72" HorizontalAlignment="Left" Margin="46,520,0,0" Name="button1" VerticalAlignment="Top" Width="359" Click="button1_Click" />

53

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,467,0,0" Name="textBlock3" Text= "Keyboard Deployed:" VerticalAlignment="Top" Width="200" />

54

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,467,0,0" Name="txtKeyboardDeployed" Text="" VerticalAlignment="Top" Width="219" />

55

<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,359,0,0" Name="textBlock1" Text="Maximum Memory:" VerticalAlignment="Top" Width="200" />

56

<TextBlock Height="30" HorizontalAlignment="Left" Margin="218,359,0,0" Name="txtMaximuMemory" Text="" VerticalAlignment="Top" Width="219" />

57

</Grid>

58

</Grid>

59

 

60

<!--Sample code showing usage of ApplicationBar-->

61

<!--<phone:PhoneApplicationPage.ApplicationBar>

62

<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

63

<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>

64

<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>

65

<shell:ApplicationBar.MenuItems>

66

<shell:ApplicationBarMenuItem Text="MenuItem 1"/>

67

<shell:ApplicationBarMenuItem Text="MenuItem 2"/>

68

</shell:ApplicationBar.MenuItems>

69

</shell:ApplicationBar>

70

</phone:PhoneApplicationPage.ApplicationBar>-->

71

 

72

</phone:PhoneApplicationPage>

clip_image004
Now let us start with our code to get the device information and load it to the respective text blocks one by one as shown in the code below. We can use the Device Status class to get the details of the device properties except the device unique ID. We will write the code later to get the unique id of the device.

Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Net;

05

using System.Windows;

06

using System.Windows.Controls;

07

using System.Windows.Documents;

08

using System.Windows.Input;

09

using System.Windows.Media;

10

using System.Windows.Media.Animation;

11

using System.Windows.Shapes;

12

using Microsoft.Phone.Controls;

13

using Microsoft.Phone.Info;

14

using System.Windows.Threading;

15

 

16

namespace F5debugWp7DeviceInformation

17

{

18

public partial class MainPage : PhoneApplicationPage

19

{

20

// Constructor

21

public MainPage()

22

{

23

InitializeComponent();

24

}

25

 

26

private void button1_Click(object sender, RoutedEventArgs e)

27

{

28

txtCurrentMemory.Text = DeviceStatus.ApplicationCurrentMemoryUsage.ToString();

29

txtMaximuMemory.Text = DeviceStatus.ApplicationMemoryUsageLimit.ToString();

30

txtPeakMemory.Text = DeviceStatus.ApplicationPeakMemoryUsage.ToString();

31

txtTotalMemory.Text = DeviceStatus.DeviceTotalMemory.ToString();

32

 

33

txtDeviceManufacturer.Text = DeviceStatus.DeviceManufacturer.ToString();

34

txtDeviceName.Text = DeviceStatus.DeviceName.ToString();

35

 

36

txtFirmwareVersion.Text = DeviceStatus.DeviceFirmwareVersion.ToString();

37

txtHardwareVersion.Text = DeviceStatus.DeviceHardwareVersion.ToString();

38

 

39

txtKeyboardDeployed.Text = DeviceStatus.IsKeyboardDeployed.ToString();

40

txtkeyboardinstalled.Text = DeviceStatus.IsKeyboardPresent.ToString();

41

 

42

txtPowerSource.Text = DeviceStatus.PowerSource.ToString();

43

 

44

//txtDeviceUniqueID.Text = "";

45

}

46

}

47

}

clip_image006
Now let us write the code for getting the Device Unique ID as shown in the code below. We use the DeviceExtendedProperties property TryGetValue to get the Unique ID, since we get it as a byte array we do a conversion to the string and assign it to the text block as shown below.

Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Net;

05

using System.Windows;

06

using System.Windows.Controls;

07

using System.Windows.Documents;

08

using System.Windows.Input;

09

using System.Windows.Media;

10

using System.Windows.Media.Animation;

11

using System.Windows.Shapes;

12

using Microsoft.Phone.Controls;

13

using Microsoft.Phone.Info;

14

using System.Windows.Threading;

15

 

16

namespace F5debugWp7DeviceInformation

17

{

18

public partial class MainPage : PhoneApplicationPage

19

{

20

public static string val;

21

 

22

// Constructor

23

public MainPage()

24

{

25

InitializeComponent();

26

}

27

 

28

private void button1_Click(object sender, RoutedEventArgs e)

29

{

30

txtCurrentMemory.Text = DeviceStatus.ApplicationCurrentMemoryUsage.ToString();

31

txtMaximuMemory.Text = DeviceStatus.ApplicationMemoryUsageLimit.ToString();

32

txtPeakMemory.Text = DeviceStatus.ApplicationPeakMemoryUsage.ToString();

33

txtTotalMemory.Text = DeviceStatus.DeviceTotalMemory.ToString();

34

 

35

txtDeviceManufacturer.Text = DeviceStatus.DeviceManufacturer.ToString();

36

txtDeviceName.Text = DeviceStatus.DeviceName.ToString();

37

 

38

txtFirmwareVersion.Text = DeviceStatus.DeviceFirmwareVersion.ToString();

39

txtHardwareVersion.Text = DeviceStatus.DeviceHardwareVersion.ToString();

40

 

41

txtKeyboardDeployed.Text = DeviceStatus.IsKeyboardDeployed.ToString();

42

txtkeyboardinstalled.Text = DeviceStatus.IsKeyboardPresent.ToString();

43

 

44

txtPowerSource.Text = DeviceStatus.PowerSource.ToString();

45

 

46

string strUniID = GetDeviceUniqueID();

47

 

48

txtDeviceUniqueID.Text = strUniID.ToString();

49

}

50

 

51

public string GetDeviceUniqueID()

52

{

53

byte[] getresult = null;

54

object uniqueId;

55

if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))

56

{

57

getresult = (byte[])uniqueId;

58

}

59

val = Convert.ToBase64String(getresult);

60

return val;

61

}

62

 

63

}

64

}

clip_image008

Now we are done with our code, let us build and execute the application by pressing F5 directly from the keyboard and we can see the application loaded to the Windows Phone 7 Emulator as shown in the screen below with the expected output.

clip_image010

Conclusion:

So in this article we have seen how to get the device information details and the device unique id using the Device Status class in detail.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter