Proposed Answer Event or flag to see if Kinect is plugged in

  • Monday, April 09, 2012 2:08 AM
     
     
    I want my program to alert if no kinect is found and then non-blockingly tell me when the user plugs in the kinect to the pc. What's the easiest way to do this. If it matters, i'm using the Beta 2 sdk rather than the newest version. Thanks!

All Replies

  • Monday, April 09, 2012 5:34 PM
     
      Has Code

    Hey,

    I'm using the actual SDK but it should also works with older SDK's. The HRESULT of NuiCreateSensorByIndex can be used:

    hr = NuiCreateSensorByIndex(0, &m_pNuiSensor);
    
    if (FAILED(hr))
        cout << "no kinect";

    You can run this code in a loop until a Kinect is found.

  • Monday, April 09, 2012 6:01 PM
    Owner
     
     Proposed Answer Has Code

    The following answer applies to v1.0 of the SDK, not Beta 2:

    You didn't indicate whether you're using native code or managed code. If you are using native code, setup a callback function as follows:

    void CALLBACK OnSensorStatusChanged( HRESULT hrStatus, const OLECHAR* instanceName, const OLECHAR* uniqueDeviceName, void* pUserData )
    {
        if (hrStatus == S_OK)
        {
            INuiSensor* pSensor = nullptr;
            if (SUCCEEDED(NuiCreateSensorById(instanceName, &pSensor)))
            {
                // pSensor represents a newly-connected sensor
    
                // ...
    
                // Done with sensor
                pSensor->Release();
            }
        }
    }
    

    When your application starts up, register your callback as follows:

    NuiSetDeviceStatusCallback(OnSensorStatusChanged, nullptr);

    If you need some kind of context in your callback function, pass a different value in place of nullptr; it will be passed to your callback function via the pUserData parameter.

    John
    K4W Dev

    • Proposed As Answer by ykbharat Sunday, April 15, 2012 1:32 PM
    •  
  • Monday, April 09, 2012 6:20 PM
    Owner
     
     Proposed Answer Has Code

    For managed code, your callback should be:

    void OnSensorStatusChanged(object sender, StatusChangedEventArgs e)
    {
        if (e.Status == KinectStatus.Connected)
        {
            // e.Sensor represents a newly-connected sensor
    
            // ...
        }
    }
    

    To register the callback at startup:

    KinectSensor.KinecSensors.StatusChanged += OnSensorStatusChanged;
    
    John
    K4W Dev
    • Proposed As Answer by ykbharat Sunday, April 15, 2012 1:32 PM
    •