Saturday, July 5, 2014

Working with accelerometer Windows devices (8.1)

Previously on windows 8 developers can access the accelerometer function on windows devices with  the library Windows.Devices.Sensors 


using Microsoft.Devices.Sensors;

But with the Microsoft come up with the Windows 8.1  and Windows Phone 8.1 this Microsoft.Devices.Sensors library is missing and the methods that developers used are slightly changed with new library. 

Windows 8.1 and Windows Phone 8.1 developers can access the normal accelerometer with expanded Windows.Devices.Sensors library
 
using Windows.Devices.Sensors;

With this code it will demonstrate about the Windows Phone App. But this method is universal among Windows 8.1 and Windows Phone 8.1 Apps

Lets Begin .....

1. Define the Accelerometer and initialize it for initialize use Accelerometer.GetDefault()method

public Accelerometer accelerometer { get; set; }
accelerometer = Accelerometer.GetDefault();

2. Cehck the accelometer is working or not and f it is avilable create Accelerometer readig capturing event

if (accelerometer != null)
               {
                   // Select a report interval that is both suitable for the purposes of the app and supported by the sensor.
                   // This value will be used later to activate the sensor.
                   uint minReportInterval = accelerometer.MinimumReportInterval;
                   desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
                   accelerometer.ReportInterval = desiredReportInterval;
                   //add event for accelerometer readings
                   accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
               }
               else
               {
                   MessageDialog ms = new MessageDialog("No accelerometer Found");
                   ms.ShowAsync();
               }


developer can assign any value to desiredReportInterval. to archive highest efficiency here we using method to get the minimum interval between two accelerometer report feeds.

With UI , its having three text boxes that named as corX,corY and corZ  to show the results of accelerometer readings. with the accelerometer reading event you can get the readings as follow

/// <summary>
        /// reading accelerometer changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                AccelerometerReading reading = args.Reading;
                corX.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                corY.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                corZ.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
            });
        }


Here is Screen shot

























Full Code In Media Fire 
http://bit.ly/1xxeQhn

 




0 comments: