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.
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
Much like the IsKeyboardDeployed property, PowerSource is another property that can change at any time. Create an event handler like this:
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:
Dispatcher.BeginInvoke(() => PowerSource.Text = DeviceStatus.PowerSource.ToString());
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:
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!