To receive gyroscope data from a SmartGlass-enabled device in an Xbox One game or app, you first start sending data to the Xbox One console from the gyroscope in a SmartGlass companion or native app that runs on the device. When the SmartGlass-enabled device sends gyroscope data to the Xbox One console, your game or app receives an Gyrometer::ReadingChanged (Gyrometer.onreadingchanged in JavaScript) event. You specify an event handler for that event to receive the gyroscope data and process it according to the needs of your game or app.
Create a method or function that you want to run when the Gyrometer::ReadingChanged (Gyrometer.onreadingchanged in JavaScript) event occurs to indicate that the gyroscope readings changed.
C++
// Declarations from the header file.
void OnGyrometerReading(Gyrometer^ sender,
GyrometerReadingChangedEventArgs^ args);
float _latestX;
float _latestY;
float _latestZ;
// In the constructor for the SGDevice class, set the initial values for the
// x. y, and z components of the gyroscope reading.
_latestX = 0.0f;
_latestY = 0.0f;
_latestZ = 0.0f;
// Implement the event handler method.
void SGDevice::OnGyrometerReading(Gyrometer^ sender,
GyrometerReadingChangedEventArgs^ args)
{
_latestX = static_cast<float> (args->Reading->AngularVelocityX);
_latestY = static_cast<float> (args->Reading->AngularVelocityY);
_latestZ = static_cast<float> (args->Reading->AngularVelocityZ);
}
// This anonymous function is part of the statement that assigns the event
// handler to the event in step 2.
function (args)
{
// Get the X, Y, and Z components of the gyrometer reading.
var X = args.reading.angularVelocityX;
var Y = args.reading.angularVelocityY;
var Z = args.reading.angularVelocityZ;
// Perform additional processing of the gyroscope values here.
};
Attach the event handler method or function to the Gyrometer::ReadingChanged (Gyrometer.onreadingchanged in JavaScript) event. In this example, the code gets the Gyrometer object by accessing the SmartGlassDevice::HtmlSurface (SmartGlassDevice.htmlSurface in JavaScript), SmartGlassHtmlSurface::Sensors (SmartGlassHtmlSurface.sensors in JavaScript), and SmartGlassSensors::Gyrometer (SmartGlassSensors.gyrometer in JavaScript) properties in succession.
C++
device->HtmlSurface->Sensors->Gyrometer->ReadingChanged +=
ref new TypedEventHandler<Gyrometer^,
GyrometerReadingChangedEventArgs^>(this,
&SGDevice::OnGyrometerReading);
// Note that the anonymous function from step 1 is assigned to the
// onreadingchanged event in this statement.
device.htmlSurface.sensors.gyrometer.onreadingchanged = function (args)
{
// Get the X, Y, and Z components of the gyrometer reading.
var X = args.reading.angularVelocityX;
var Y = args.reading.angularVelocityY;
var Z = args.reading.angularVelocityZ;
// Perform additional processing of the gyroscope values here.
};