Visual Basic Windows Phone 7 Series #11. How to create an accelerometer for Windows Phone 7

Previously, I explained how to create a panorama and pivot effect for Windows Phone 7. In this blog post, I want to share a sample that will help you to create an accelerometer for Windows Phone 7. An accelerometer is a device that measures the proper acceleration of the device. It is a sensor that measures acceleration forces caused by moving the sensor. In this application the accelerometer feature will allow you to determine the orientation and the motion of the phone.

Now I will demonstrate how to create an accelerometer for Windows Phone 7, using Visual Basic for Windows Phone Developer Tools. Sounds interesting? So let’s begin.

But hey, wait a minute! Before you create the accelerometer application, you need to install the following applications:

Visual Studio 2010 Professional, Premium,
or Ultimate. If you do not have any of the releases, you can download any of
the following trial versions as per your preference: Professional, Premium, or Ultimate.

Note: This method controls the accelerometer. If initially the accelerometer is null, it is initialized and started. And if the accelerometer value is not null, then it is stopped. This method also defines a try catch block that displays errors and exceptions appropriately.

To create an event handler that handles the change in readings, add the following code:

#Region"Accelerometer Event Handling"

''' <summary>

''' The event handler for the accelerometer ReadingChanged event.

''' BeginInvoke is used to pass this event args object to the UI thread.

Note: This method handles the change of reading for the accelerometer as well as handles the change of reading on the UI thread. This enables to display the reading value on the screen.

There you are! Now your accelerometer application for Windows Phone 7 is ready! You just need to build and debug the application.

Step 3 - Build and debug the application

To build the application, select Build > Build Solution. The project should build without any errors. If there are errors, check the earlier steps, correct the errors, and then build the application again.

To debug the application, set the deployment target of the application to “Windows Phone 7 Emulator”.

Select Debug > Start Debugging. The emulator window is displayed.

Do you want to test the accelerometer settings? No problem, click “On” and presto! The status of accelerometer changes. The reading of the coordinate change as the phone’s orientation and position is changed.

Step4 - Rebuild in the release mode before publishing

On the standard toolbar, change the configuration manager to Release.

To rebuild the application, select Build > Rebuild. The XAP file of the application is generated, which is the package that you will have to submit in the marketplace to publish the application. You can also locate this XAP file in the Bin\Release folder.

Summary

That’ it! Wasn’t that amazing? You have successfully created an accelerometer for Windows Phone 7, that too in just 4 steps! It’s pretty easy, right? I am sure you must have enjoyed and had a lot of fun creating this application.

You can find the full source code for the accelerometer application here. This application uses general Silverlight and Visual Basic features that are applicable for different application types including Windows Phone application.