Receiving Messages from a SmartGlass-Enabled Device in an Xbox One Game or App

To receive messages from a SmartGlass-enabled device in an Xbox One console game or app, create an event handler that you want to run to process the messages when the SmartGlassHtmlSurface::MessageReceived (SmartGlassHtmlSurface.onmessagereceived in JavaScript) event occurs and attach that event handler to the event.

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

  1. In the code for your SmartGlass companion or Native app, include statements that send messages to the Xbox One console.
    For information about how to send a message from a SmartGlass companion to an Xbox One console, see “Sending Messages from a SmartGlass Companion to an Xbox One Console” in the SmartGlass Companion SDK documentation. For information about how to send a message from a Native app to an Xbox One console, see “Sending a Message to a Title” in the SmartGlass Native SDK documentation.
  2. Create an event handler that you want to use to process that message when the SmartGlassHtmlSurface::MessageReceived (SmartGlassHtmlSurface.onmessagereceived in JavaScript) event occurs.

    C++

    void SGDevice::OnMessageReceived(SmartGlassHtmlSurface^ /*pHtmlSurface*/, 
        SmartGlassMessageReceivedEventArgs^ /*pValue*/)
    {
        // Process messages from the SmartGlass device here.
    }  
    
    // This anonymous function is assigned to the onMessageReceived event in 
    // the statement in step 2.
    function (args)
    { 
          
        // Show the message that was sent from the device.
        trace("Message from the device: " + args.message);
          
        // Convert the message to an object.
        var obj = JSON.parse(args.message);
          
       // Process the properties of the object as needed here.
          
    }  
    
  3. Attach the event handler that you defined in step 1 to the SmartGlassHtmlSurface::MessageReceived (SmartGlassHtmlSurface.onmessagereceived in JavaScript) event.

    C++

    device->HtmlSurface->MessageReceived += ref new 
        TypedEventHandler<SmartGlassHtmlSurface^, 
        SmartGlassMessageReceivedEventArgs^>(this, 
        &SGDevice::OnMessageReceived);  
    
    // Note that the anonymous function from step 1 is assigned to the 
    // onmesssagerecevied event here.
    device.htmlSurface.onmessagereceived = function (args)
    { 
          
        // Show the message that was sent from the device.
        trace("Message from the device: " + args.message);
          
        // Convert the message to an object.
        var obj = JSON.parse(args.message);
          
       // Process the properties of the object as needed here.
          
    };  
    

See also

Sending Messages from an Xbox One Console to a SmartGlass-Enabled Device

SmartGlassHtmlSurface.MessageReceived