MOBILE

Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 2)

1/24/2011 4:39:13 PM

3. Coding the Application

In Solution Explorer, open MainPage.xaml.cs and replace the code there with the following C# code blocks that will implement the UI updates using accelerometer data.

3.1. Specifying the Namespaces

Begin by listing the namespaces the application will use. Notice our inclusion of Microsoft.Devices.Sensors that will allow you to start and stop Windows Phone's accelerometer.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
namespace CaptureAccelerometerData
{
public partial class MainPage : PhoneApplicationPage
{

3.2. Initializing Variables

The variable _ac, an Accelerometer object, will be used to start and stop, and retrieve x, y, z and time. Also notice the inclusion of the ReadingChanged event, which you'll draw on to send captured accelerometer data to your UI.

Accelerometer _ac;

public MainPage()
{
InitializeComponent();

_ac = new Accelerometer();
_ac.ReadingChanged += new
EventHandler<AccelerometerReadingEventArgs>(_ac_ReadingChanged);
}

3.3. Capturing and Displaying Accelerometer Data

Notice here that you cannot directly change the UI elements upon receiving the accelerometer data because the accelerometer data comes from a different thread than the current UI thread. If you try to change the UI elements directly here you will get an "Invalid cross-thread access" error, as shown in Figure 6-13. In order to overcome this problem, you must use the Dispatcher in the current UI thread, as shown in the following code.

Figure 4. Invalid cross-thread access error

private void ProcessAccelerometerReading(AccelerometerReadingEventArgs e)
{
txtTime.Text = e.Timestamp.ToString();
txtX.Text = e.X.ToString();
txtY.Text = e.Y.ToString();
txtZ.Text = e.Z.ToString();
txtPitch.Text = RadianToDegree((Math.Atan(e.X / Math.Sqrt(Math.Pow(e.Y, 2) +
Math.Pow(e.Z, 2))))).ToString();
txtRoll.Text = RadianToDegree((Math.Atan(e.Y / Math.Sqrt(Math.Pow(e.X, 2) +
Math.Pow(e.Z, 2))))).ToString();
txtTheta.Text = RadianToDegree((Math.Atan(Math.Sqrt(Math.Pow(e.X, 2) +
Math.Pow(e.Y, 2))/ e.Z))).ToString();
}


3.4. Implementing Start and Stop of Accelerometer

Implement the button event for stopping and starting the accelerometer. Notice here that you must anticipate the possible error that might occur when you are trying to start or stop the accelerometer.

private void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
_ac.Start();
}
catch (AccelerometerFailedException)
{
MessageBox.Show("Acceleromter failed to start.");
}
}

private void btnStop_Click(object sender, RoutedEventArgs e)
{
try
{
_ac.Stop();
}
catch (AccelerometerFailedException)
{
MessageBox.Show("Acceleromter failed to stop.");
}
}
}
}


4. Testing the Finished Application

To test the finished application, press F5. The result should resemble the screenshot shown in Figure 6-10, and you will see that the x, y, z, and time text blocks are constantly being updated each time you click the Start button. Remember that to run the application on a Windows Phone 7 device, you must choose the "Windows Phone 7 Device" option shown in Figure 5.

Figure 5. Choosing a Windows Phone 7 device before running the application

Other  
  •  Using Windows Phone 7 Technologies : Understanding Orientation and Movement
  •  Programming the Mobile Web : HTML 5 (part 4) - Client Storage
  •  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
  •  Programming the Mobile Web : HTML 5 (part 2) - The canvas Element
  •  Programming the Mobile Web : HTML 5 (part 1)
  •  Windows Phone 7 : Submitting Your First Windows Phone Application to the Windows Phone Marketplace
  •  Windows Phone 7 : Packaging, Publishing, and Managing Applications
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 3)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 1)
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 2) - JavaScript Mobile UI Patterns
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 1) - JavaScript UI Libraries
  •  Windows Mobile Security - Kernel Architecture
  •  Windows Mobile Security - Introduction to the Platform
  •  iPhone Programming : Table-View-Based Applications - Building a Model
  •  Mobile Application Security : The Apple iPhone - Push Notifications, Copy/Paste, and Other IPC
  •  Mobile Application Security : The Apple iPhone - Networking
  •  Windows Phone 7 Development : Handling Device Exceptions
  •  Registering a Windows Phone Device for Debugging
  •  Programming the Mobile Web : WebKit CSS Extensions (part 5) - Transformations
  •  
    Most View
    Windows Vista : Internet Me (part 3) - Control Your PC Remotely,Manage the Nameserver Cache
    Samsung Galaxy Camera VS The Rest (Part 4)
    Introducing Windows Presentation Foundation and XAML : Understanding The Syntax of WPF XAML (part 1)
    Creative Sound BlasterAxx SBX 20
    So What Is ‘Cloud’ And Why Should I Care? (Part 1)
    Asus Rog Tytan CG8565 - Clash Of The Tytan
    Powered By Windows (Part 3) - Canon LV-8320 LCD Projector & ASUS N-series Mystic Edition
    Improve Your Mac (Part 5) - Using little snitch to identify bandwidth hogs
    Editor’s Picks: Tablet Photo-Editing Apps (Part 2) - Google Picasa 3.9, Nik Software Snapseed Desktop, Serif Photoplus X5
    Mobile - The Good, The Budget And The Surprising
    Top 10
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 7)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 6)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 5) : Thermaltake Armor Revo
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 4) : Thermaltake Level 10 GTS
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 3) : Thermaltake Commander MS-III
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 2)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 1) : Thermaltake Commander MS-I
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 3)
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 2)
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 1)