Receiving Notifications about the Addition and Removal of SmartGlass-Enabled Devices

In a SmartGlass experience, an Xbox One console and one or more SmartGlass devices interact. The SmartGlass device initiates the connection. A title that runs on the Xbox One console can track the SmartGlass devices that connect to and disconnect from the Xbox One console by implementing and attaching event handlers for the events of the SmartGlassDeviceWatcher class.

To receive notifications about the addition and removal of SmartGlass-enabled devices in an Xbox One game or app

  1. Create a new SmartGlassDeviceWatcher object.

    C++

    SmartGlassDeviceWatcher^ _deviceWatcher;
          
    _deviceWatcher = ref new SmartGlassDeviceWatcher();  
    
    // SmartGlass stuff
    var watcher;
          
    // Create a SmartGlassDeviceWatcher object.
    watcher = new Windows.Xbox.SmartGlass.SmartGlassDeviceWatcher();  
    
  2. Implement the event handlers that you want to run when the SmartGlassDeviceWatcher::DeviceAdded (SmartGlassDeviceWatcher.deviceAdded in JavaScript) and SmartGlassDevcieWatcher::DeviceRemoved (SmartGlassDeviceWatcher.deviceRemoved in JavaScript) events occur.

    C++

    void OnDeviceAdded(SmartGlassDeviceWatcher^ sender, SmartGlassDevice^ device);
    void OnDeviceRemoved(SmartGlassDeviceWatcher^ sender, SmartGlassDevice^ device);
          
    void SGDeviceCollection::OnDeviceAdded(SmartGlassDeviceWatcher^ sender, 
        SmartGlassDevice^ device)
    {
        SGDevice^ sgDevice = ref new SGDevice(device);
        _devices.push_back(sgDevice);
          
        // Send the last known state to the new device
        size_t index = _devices.size() - 1;
        _devices[index]->SendGameState(_lastKnownState, GetControllerType(index));
    }
          
    void SGDeviceCollection::OnDeviceRemoved(SmartGlassDeviceWatcher^ sender, 
        SmartGlassDevice^ device)
    {
        _devices.erase(
            std::remove_if(_devices.begin(), _devices.end(),
            [device](SGDevice^ i)
            {
                return i->Id == device->Id;
            }), _devices.end());
    }  
    
    function sgDeviceAdded(device) 
    {
        // Log the device.
        trace("Device added: " + device.displayName + "[" + device.id + "]");
          
        // Add the device to the list of known devices.
        deviceList.push(device);
          
        // Perform additional tasks for the device.
    }  
    
  3. Attach the event handlers that you implemented in step 2 to the SmartGlassDeviceWatcher::DeviceAdded (SmartGlassDeviceWatcher.deviceAdded in JavaScript) and SmartGlassDevcieWatcher::DeviceRemoved (SmartGlassDeviceWatcher.deviceRemoved in JavaScript) events.

    C++

    _deviceWatcher->DeviceAdded += 
        ref new TypedEventHandler<SmartGlassDeviceWatcher^, 
        SmartGlassDevice^>(this, &SGDeviceCollection::OnDeviceAdded);
    _deviceWatcher->DeviceRemoved += 
        ref new TypedEventHandler<SmartGlassDeviceWatcher^, 
        SmartGlassDevice^>(this, &SGDeviceCollection::OnDeviceRemoved);  
    
    // When a device is added or removed
    watcher.ondeviceadded = sgDeviceAdded;
    watcher.ondeviceremoved = sgDeviceRemoved;  
    

See also

SmartGlassDeviceWatcher

DeviceAdded

DeviceRemoved