Accelerometer.ReadingChanged | readingchanged event

Applies to Windows and Windows Phone

Occurs each time the accelerometer reports a new sensor reading.

Syntax


function onReadingChanged(eventArgs) { /* Your code */ }
 
// addEventListener syntax
accelerometer.addEventListener("readingchanged", onReadingChanged);
accelerometer.removeEventListener("readingchanged", onReadingChanged);
 
- or -

accelerometer.onreadingchanged = onReadingChanged;


Event information

Delegate TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>

Remarks

An application may register this event handler to obtain sensor readings. The application must establish a desired ReportInterval. This informs the sensor driver that resources should be allocated to satisfy the requirements of the application.

The following example demonstrates how a Windows Store app built with C# and XAML registers its ReadingChanged event handler.


private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_accelerometer != null)
    {
        // Establish the report interval
        _accelerometer.ReportInterval = _desiredReportInterval;

        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;
    }
    else
    {
        rootPage.NotifyUser("No accelerometer found", NotifyType.StatusMessage);
    }
}


The following example shows the ReadingChanged event handler.


async private void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        AccelerometerReading reading = e.Reading;
        ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
        ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
        ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
    });
}


The following example demonstrates how a Windows Store app built with JavaScript registers its ReadingChanged event handler.


function enableReadingChangedScenario() {
    if (accelerometer) {
        // Set the reportInterval to enable the sensor events
        accelerometer.reportInterval = reportInterval;

        document.addEventListener("msvisibilitychange", msVisibilityChangeHandler, false);
        accelerometer.addEventListener("readingchanged", onDataChanged);
        document.getElementById("scenario1Open").disabled = true;
        document.getElementById("scenario1Revoke").disabled = false;
    } else {
        WinJS.log && WinJS.log("No accelerometer found", "sample", "error");
    }
}


The following example shows the ReadingChanged event handler.


function onDataChanged(e) {
    var reading = e.reading;

    document.getElementById("eventOutputX").innerHTML = reading.accelerationX.toFixed(2);
    document.getElementById("eventOutputY").innerHTML = reading.accelerationY.toFixed(2);
    document.getElementById("eventOutputZ").innerHTML = reading.accelerationZ.toFixed(2);
}


Requirements

Minimum supported client

Windows 8

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8

Namespace

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

Metadata

Windows.winmd

See also

Accelerometer

 

 

Show:
© 2014 Microsoft