Retrieving gyrometer data in complex apps (Windows Runtime apps)

Applies to Windows and Windows Phone

An app can track changes in angular velocity along the x-axis, y-axis, and z-axis by registering to receive event notifications from the Gyrometer class that represents a device.

The following code examples demonstrate how to retrieve the default gyrometer for a device and then register a ReadingChanged event handler to receive notifications when the sensor issues a new reading.


gyrometer = Windows.Devices.Sensors.Gyrometer.getDefault();



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

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


The ReadingChanged event handler then receives the new values for the angular velocity along the x-axis, y-axis, and z-axis, as shown in this code example.


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

    document.getElementById("eventOutputX").innerHTML = reading.angularVelocityX.toFixed(2);
    document.getElementById("eventOutputY").innerHTML = reading.angularVelocityY.toFixed(2);
    document.getElementById("eventOutputZ").innerHTML = reading.angularVelocityZ.toFixed(2);
}


 

 

Show:
© 2014 Microsoft