31 Days of Mango | Day #2: DeviceStatus

Day2-DeviceStatusDay1-EmulatorTools

This article is Day #2 in a series called the 31 Days of Mango.

Today, we are going to talk about the DeviceStatus class, and the information we can gather from a user’s device by accessing this class.

The application descibed in this article is available in the Windows Phone marketplace.

DownloadIcon

What can we know about a user?

This is a question that I get quite often, and I generally respond with “very little.”  You’ll never have access to the phone number, email address, or identity of your user, unless they choose to provide that to you.  What we can know, however, is still useful information, even if only for usability enhancements.  Here’s a list of the data that DeviceStatus knows about:

  • Device Manufacturer
  • Device Name
  • Firmware Version
  • Hardware Version
  • Total Memory Available
  • Current Memory Usage
  • Memory Usage Limit
  • Peak Memory Usage
  • Availability of a Physical Keyboard
  • Physical Keyboard Deployment Status
  • Current Power Source

We can also set events to notify us if the keyboard or power status states change.  Let’s take a look at each of these data points, and how we can use them.  In order to access any of these properties, you will need to add a using statement to the top of your page:

using Microsoft.Phone.Info;

 

None of these properties will allow you to uniquely identify a specific device, but they will allow you to determine make, model, software version, etc.  This can come in handy if you are trying to optimize for specific hardware, for example.  In the example application for this article, we will be writing all of the values to a TextBlock with a similar name.  We will be filling in the blanks of the following screen:

image

Device Manufacturer (string)

As you would expect, this property exposes the name of the device manufacturer that the user is using.  In the emulator, this value will display as “Microsoft.”  To access this property, you can use the following syntax:

DeviceManufacturer.Text = DeviceStatus.DeviceManufacturer;

Device Name (string)

This is the specific name of the device, but not always the name you would expect.  For example, I own the HTC Arrive, which is on Sprint’s network.  However, when I run this application on my phone, the DeviceName is listed as T7575, which is the name that HTC uses for this specific device.  To grab this name from the DeviceStatus class, you can use the following code:

DeviceName.Text = DeviceStatus.DeviceName;

This is also a valuable piece of information if you want to detect when new phones start to get tested by their manufacturers. By reporting back to yourself on the different device names you find, you’ll be one of the first to know when a new device is on its way to the market.

Firmware Version (string)

Firmware version is probably not terribly interesting, because you have so many versions of firmware across so many different devices.  However, firmware is one piece of data that can indicate to you whether or not a Windows Phone update has been deployed.  For example, the firmware version for my HTC Arrive is 2305.13.40301.651, which represents the Mango release.  To access this data, use the following syntax:

FirmwareVersion.Text = DeviceStatus.DeviceFirmwareVersion;

Hardware Version (string)

As many Samsung Focus users have discovered, hardware version can mean a great deal of difference between similar devices.  In case you haven’t heard about it, there are Focus phones designated at 1.3 and 1.4 versions.  Apparently the 1.3 Focus phones have a greater challenge receiving updates, but ultimately, they are the same phone.  In order to determine what hardware version your user is using, you can use the following code:

HardwareVersion.Text = DeviceStatus.DeviceHardwareVersion;

Device Total Memory (long)

This is the physical amount of RAM that is available on the user’s device.  It’s not going to read the same value that the device claims on the side of the box, instead it will be the available RAM for your device, after the OS takes its approximate 166 MB of memory, plus any other memory that has been allocated for other apps running in the background.  To determine this number, use the following syntax:

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

Application Current Memory Usage (long)

For applications that use a significant amount of memory to operate, it’s important to monitor just how much memory you have allocated.  The limit is 90MB, so it’s important to make sure that you aren’t going to exceed that number.  To determine your app’s current memory usage, use this syntax:

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

Application Memory Usage Limit (long)

When building memory-intensive applications, this number is an important one to pay attention to.  The ApplicationMemoryUsageLimit is the maximum additional amount of memory that your current application process can use.  This value is returned as the number of bytes available:

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

Application Peak Memory Usage (long)

Another property related to memory management, this property allows you to determine what your peak memory usage has been.  If you were monitoring the previous two properties closely, then you likely already know this value, but it’s still a valuable piece of information while you’re debugging your application.  To get this value, use the following code:

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

Is Keyboard Present (boolean)

This might seem a little silly to check, but there’s many reasons why you might want to know if a user has a physical keyboard available on their device.  Perhaps you want to use it as a directional game pad, or as soft keys for a multiple choice quiz game.  Knowing if the user has a keyboard lets you take advantage of the extra hardware in a variety of ways.  To access this value, use the following code:

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

I’m interested to know how you would use this piece of data, so please chime in with a comment about what your vision for keyboard detection would be.

Is Keyboard Deployed (boolean)

“Deployed” might be a grandiose word for sliding out a keyboard on your phone, but we can certainly check to determine if the keyboard has been deployed.  Much like knowing whether a phone even has a keyboard, this is a niche piece of knowledge.  However, you may find it important to know whether a user has their keyboard out or not, and this is the way to detect it:

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

Because this value can change at any time, we also have the ability to monitor this value with an event handler.  To do this, you need to define the event handler when you start your application like this:

DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged);

The event handler method for this is just another manual check of the property we checked at the beginning of this section.  The only difference is that this time, we need to do it on a separate thread.  To do this, out method would look like this:

void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(() => IsKeyboardDeployed.Text = DeviceStatus.IsKeyboardDeployed.ToString());
}
 

Power Source (object)

Power source is perhaps the most valuable item in this entire list.  Having the ability to know when your user has their phone plugged in vs. running on battery can change how you approach your application’s activities.  Gathering a ton of new images or data while your user is on battery power might not be the most courteous decision, just as expecting them to accurately play a game using the accelerometer might be difficult if their phone is currently plugged in.  There’s loads of reasons to know what power option your user is using, and here’s how to detect it:

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

Much like the IsKeyboardDeployed property, PowerSource is another property that can change at any time.  Create an event handler like this:

DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged);

And the method you need for this event also requires thread safety, so we will use the Dispatcher.BeginInvoke method to move this process to a separate thread:

void DeviceStatus_PowerSourceChanged(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(() => PowerSource.Text = DeviceStatus.PowerSource.ToString());
}
 

Summary

The DeviceStatus class gives us access to a wealth of information about our user’s device, but it’s just vague enough to not be wildly valuable.  That being said, each of these properties has a distinct value of their own, and in the right circumstances, a developer will be overly thankful that they can obtain this information from their user’s devices. 

For a final peek at the working application we just created, here’s a screenshot from the Windows Phone emulator:

image

To download this code as a working project that you can deploy to your own phone, click the Visual Studio icon below.  Each post in this series will have a companion set of code, so make sure to download it and play with the code to understand the topic completely!

Tomorrow, we’re going to dive into a much more user-friendly topic, Alarms and Reminders.  I will walk you through exactly how to set reminders on your user’s device, even if your app isn’t running.  See you then!

toolsbutton

18 thoughts on “31 Days of Mango | Day #2: DeviceStatus

  1. Pingback: 31 Days of Mango « Blankenblog

  2. And what about battery power? Can we get it’s percentage? Or is an event exist if the power reach a critical level?

    • I looked into this when I wrote the article, and no, there is not a battery API that lets us get things like battery percentage or level. That will hopefully come in a future release, but I have no visibility into that.

  3. Pingback: 31 Days of Mango | Day #3: Alarms and Reminders « Blankenblog

  4. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #973

  5. Pingback: 31 Tage Mango | Tag #2: DeviceStatus | leitning.de

  6. Pingback: 31 Days of Mango | Day #5: Gyroscope « Blankenblog

  7. Good article. Good series. Just want to say thank you. They are very useful for me.

  8. Pingback: Osman Gafarov : 31 день с Mango. День #2. Класс DeviceStatus

  9. Pingback: День #2 « Rybin Dmitry's Blog

  10. Pingback: La Liga Silverlight | Día 2: Estado del dispositivo

  11. Pingback: WP Astuces » Compte rendu des 31 jours de Windows Phone Mango – Partie 1

  12. About DeviceStatus.IsKeyboardPresent – one place where this would be usefull is in chat/IM applications, where if a hardware keyboard is present, it can optimize the screen for keyboard input: the Send button can be hidden and Enter could be used instead etc..

  13. wonderfully explained …
    About DeviceStatus.IsKeyboardPresent – one situation where this value can be useful is in image display s/w …

  14. Nice one. Thanks. It helped.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s