Thursday 23 January 2014

Android Accelerometer vs Windows Accelerometer part 2

We have accessed accelerometer readings on our android devices why don't we see how this is done on a windows phone development environment right quick and easy.

Create your windows phone project; NOTE this will work on a windows phone 7 device too so you can select the windows phone 7 emulator, as a matter of fact i will be running this on a wp7 emulator.

First we drag a TextBlock and name it readings(you can name it what ever) note i also changed my default text to 0.0, because i felt it way cooler...
<textblock horizontalalignment="Left" margin="79,106,0,0" name="reading" text="0.00" textwrapping="Wrap" verticalalignment="Top">
</textblock>

Next you need to add these references:

using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;

If you can't find these references just right click references and select add references, you will get a list of references select those to.
We then have to create our Accelerometer variable
Accelerometer accelerometer;

with that setup the next thing we need to do is check if our device has an accelerometer sensor
if (!Accelerometer.IsSupported)
            {
                MessageBox.Show("No Accelerometer sensor found");
            }

now we have checked for our sensor and given the user a message if his device does not have an accelerometer, we then have to initialize our accelerometer variable, set the update/refresh time, create our event handler and then start our accelerometer see code below
if (accelerometer == null)
            {
                //initialize the accelerometer variable
                accelerometer = new Accelerometer();
                //set the update delay in milliseconds
                accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(10);
                accelerometer.CurrentValueChanged += new EventHandler>(accelerometer_ValueChanged);
                //start our sensor
                accelerometer.Start();
            }

so if there is an accelerometer device and we are not getting any data (that is, if it is offline) then we do the above.
we then update our UI thread with the following code:
void accelerometer_ValueChanged(object sender, SensorReadingEventArgs e)
        {
            Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading));
        }

Great you have it this far, believe it or not we are getting our data from the accelerometer we for those with doubt why don't we display this data in our TextBlock through our UpdateUI method.
private void UpdateUI(AccelerometerReading accelerometerReading)
        {
            Vector3 acceleration = accelerometerReading.Acceleration;

            reading.Text = "X: " + acceleration.X.ToString() + "\nY:" + acceleration.Y.ToString() + "\nZ: " + acceleration.Z.ToString();
        }

You can now run and test the code in the emulator, if you don't know how this is done just run the code and click the double arrow button pointing towards the right you will see a new window with some tabs, select accelerometer tab and drag the orange circle in the middle of the virtual phone.
you should see your readings
Full 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;
using Microsoft.Xna.Framework;

namespace PhoneApp3
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer accelerometer;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
            if (!Accelerometer.IsSupported)
            {
                MessageBox.Show("No Accelerometer");
            }
            if (accelerometer == null)
            {
                accelerometer = new Accelerometer();
                accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(10);
                accelerometer.CurrentValueChanged += new EventHandler>(accelerometer_ValueChanged);
                accelerometer.Start();
            }

            
        }

        void accelerometer_ValueChanged(object sender, SensorReadingEventArgs e)
        {
            Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading));
        }

        private void UpdateUI(AccelerometerReading accelerometerReading)
        {
            Vector3 acceleration = accelerometerReading.Acceleration;

            reading.Text = "X: " + acceleration.X.ToString() + "\nY:" + acceleration.Y.ToString() + "\nZ: " + acceleration.Z.ToString();
       
    }
}

Our output

No comments:

Post a Comment