Discussion on the 31st day of Windows Phone 7 Development-11th: accelerator Sensor

Source: Internet
Author: User

This article is about Windows Phone 7 development on the 31st day"11th day of the series.

Yesterday, we discussed the keyboard of Windows Phone. Today, let's talk about hardware-based acceleration sensors and how to use the information they provide.

What is an acceleration sensor?

There is no better definition at present. The accelerator sensor in Windows Phone is used to measure the acceleration of three coordinate axes. The acceleration mentioned here is relative to the acceleration of free falling body. Except for a timestamp, values are expressed by the gravity value (G) (1G = 9.81 m/s2 ). This means that if you flat the front of the phone to an absolute plane, the Z axis should be-1.0, and the data of the other two axes will be 0. The following example demonstrates different values (thanks for the images from WindowsTeamBlog ):

How can I obtain data from the acceleration sensor of WP7?

Fortunately, this is very simple. The complexity is that we need to manage some threads, but it is also very simple. The following is what we have to do:

  1. Initializes an Accelerometer object.
  2. Create a ReadingChanged () event handler to monitor data changes.
  3. Return the results from the event to our page thread (this event is triggered in another thread ).
  4. Use the data in the program.

This is all content of MainPage. xaml. cs in my example. You will see that I have created three textblocks (XText, YText, and ZText) in the MainPage. xaml file, so I can display these values on the screen. You can also see that I added a reference to the Microsoft. Devices. Sensors namespace to access the Accelerometer class.

Code Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using Microsoft. Phone. Controls;
Using Microsoft. Devices. Sensors;

Namespace Day11_Accelerometer
{
Public partial class MainPage: PhoneApplicationPage
{
Accelerometer acc = new Accelerometer ();
// Constructor
Public MainPage ()
{
InitializeComponent ();
Acc. ReadingChanged + = new EventHandler <AccelerometerReadingEventArgs> (acc_ReadingChanged );
Acc. Start ();
}

Void acc_ReadingChanged (object sender, AccelerometerReadingEventArgs e)
{
Deployment. Current. Dispatcher. BeginInvoke () => ThreadSafeAccelerometerChanged (e ));
}

Void ThreadSafeAccelerometerChanged (AccelerometerReadingEventArgs e)
{
XText. Text = e. X. ToString ("0.000 ");
YText. Text = e. Y. ToString ("0.000 ");
ZText. Text = e. Z. ToString ("0.000 ");
}
}
}

The simulator cannot simulate the acceleration sensor data.

Yes. If you download the final code in this article, you will surely wonder why the Z axis data is always-1, because the simulator thinks it is on a plane. It does not have (effective) Methods to simulate sensor data acceleration. However, some smart people have found a way to make this a reality. The rest of this article makes me very embarrassed. I would like to write an example for each simulator solution, but there are so many good ways to simulate sensor data acceleration. I think it is better to let you know this than simply writing code examples. There are many suggestions for you to experiment (each site provides sample code ):

Reactive Extensions

Reactive Extensions is a framework that allows you to simulate data for the accelerator sensor (as well as location services, which I will introduce in October 13th) without the need for real devices. In fact, you cannot control this action. It can only generate random data for you. Installation is very simple, and it is very efficient than development without a real mobile phone. This site on MSDN has a very good drill: http://bit.ly/bdeaft.

AccelKit

This may be the coolest choice. It uses cameras and augmented reality to allow you to print pictures on your mobile phone, just like moving it in the real world. It is so cool that I am so fascinated, there are two reasons:

  1. It allows you to simulate actions.
  2. It uses augmented reality, people. This is an amazing technological application.

Let's take a look at these code examples, here to download the additional library: http://bit.ly/9TfqaSn

WiimoteLib

This library can be used not only for Windows Phone simulators, but it does work very well for our purposes. The story is like this: If you have a Nintendo Wii, you can use a controller (a Wii Remote Control) to generate your accelerator sensor data. Brian Peek is a developer who integrates the content into this small hosted code library. If you want to implement this, you can go to his site to get relevant information! Http://bit.ly/aUdEEW

Windows Mobile uniied Sensor API

Yes, it is "Windows Mobile ". This is the way to interact with the accelerator sensor on some types of Windows Mobile 6.x devices. This article describes a way to use this technology for development on Windows Phone. They don't seem to provide code examples, but it looks very simple: http://bit.ly/crHbW9

Download Sample Code

Remember that all the above solutions are temporary. Until the device was obtained, the data was "forged" to accelerate the sensor. Nothing can replace running your code on a real device. If you have no chance to do this,Contact me. Let's see what we can do for you on your phone, even if it's just a few hours.

Address: http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-11-Accelerometer.aspx

 

If you like my article, click "recommendation". Thank you!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.