Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

No accelerometer support in the Windows Phone 7 emulator? Not the end.

DZone's Guide to

No accelerometer support in the Windows Phone 7 emulator? Not the end.

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

Yes, the Windows Phone 7 emulator indeed lacks accelerometer support. Although you can still develop applications for Windows Phone 7 that take advantage of the accelerometer capabilities, you cannot test them in the current version of the emulator. Given the number of existing applications that use the accelerometer as the foundation of their activity, this can be a serious development problem. So there is not that much you can do about that at the moment, or can you?

How the accelerometer activity is handled?

In a Silverlight Windows Phone 7 application, the accelerometer activity is handled via an instance of the Accelerometer class, a member of the Microsoft.Devices.Sensors namespace. The event handler that tracks the activity is ReadingChanged and it is triggered constantly to check the values for X, Y and Z positioning after a Start call being placed for the Accelerometer instance. The sensor reading is stopped when a Stop call is hit for the same accelerometer instance. Here is an example:

// Constructor
public MainPage()
{
InitializeComponent();
Accelerometer accelerometer = new Accelerometer();
accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
accelerometer.Start();
}

void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
Debug.WriteLine(string.Format("X: {0} Y: {1} Z: {2}",e.X,e.Y,e.Z));
}

For this sample, the Accelerometer will constantly read the data and write it in the Output console:

The default phone position is set to X = 0, Y = 0 and Z = -1. Basically, this means that the phone is laying like this:

This is easy to understand if you understand object positioning in a 3D grid. If you don’t want to find out the default positions on your own, you can simply visit this page for a list of basic values.

But since these values are unchangeable for now, what could you do?

Use the mouse

For testing purposes, you can use the mouse. Even though a Windows Phone 7 device has no hardware or software mouse support whatsoever, you can still use it while developing your application. Although it doesn’t provide the entire set of capabilities (for example, it lacks movement on the Z-axis), it is enough for most cases to test phone inclination to the left and right (and even up and down).

To do this, all you have to do is create a reference to the MouseMove event handler.

this.MouseMove += new MouseEventHandler(MainPage_MouseMove);

Then, you can read the cursor position directly by calling the GetPosition method:

void MainPage_MouseMove(object sender, MouseEventArgs e)
{
Debug.WriteLine(string.Format("X: {0} Y: {1}", e.GetPosition(this).X, e.GetPosition(this).Y));
}

The position is taken relatively to the current page and once you click the mouse and drag it inside the page, you can see the X and Y values being modified.

Mind you that this is in no way a replacement for the existing accelerator functionality, but since the emulator has no support for it whatsoever, this method could be considered a temporary workaround.

Is this the only way?

No. There have been other methods presented as well. For example, Bill Reiss used a WiiMote (via WiimoteLib for .NET) to simulate accelerometer data. The custom event handler as well as the custom accelerometer class itself was written by hand, so the default Accelerometer class remained intact. This method also involves connecting to a web service (a Wiimote proxy) to get the data, so there is the main drawback - the possibility of a delay between the actual obtaiend data and accelerometer data processing. Around the same idea there were experiments with getting other phones generate accelerometer data, that is later on passed to a web service and then read by a Windows Phone 7 application.

Hopefully in one of the next releases of the Windows Phone 7 emulator, we will see an implementation that will allow the simulation of accelerometer movement, at least from code-behind (being able to pass specific test accelerometer arguments, for example). For now, the only way would be either testing on a real Windows Phone 7 device or by using one of the presented workarounds.

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}