Receiving Gyroscope Data from a SmartGlass-Enabled Device

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.

To receive gyroscope data from a SmartGlass-enabled device in an Xbox One game or app

  1. In your SmartGlass companion or native app, include code that begins polling for gyroscope data and sending that data to the Xbox One console.
    For information about how to send gyroscope data from a SmartGlass companion to an Xbox One console, see “Using the Gyroscope in a SmartGlass Companion” in the SmartGlass Companion SDK documentation. For information about how to send gyroscope data from a native app to an Xbox One console, see “Sending Gyrometer Data to an Xbox One Console” in the SmartGlass Native SDK documentation.
  2. 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.
          
     };  
    
  3. 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.
          
     };  
    

See also

Gyrometer.ReadingChanged Event

Gyrometer Class

SmartGlassDevice.HtmlSurface

SmartGlassHtmlSurface.Sensors

SmartGlassSensors.Gyrometer