Detecting the Kinect sensor state change/disconnection example

Description

Today's project is a simple example, but one that will help you add that professional and completeness to your Kinect for Windows SDK applications...

[# KINECTSDK] HowTo: Detect the sensor state change and sensor disconnection

If we go ahead we can see in KinectSensor we have a special event to capture the States of sensors: StatusChanged.This event also can be used to detect when you connect or disconnect a sensor to the USB port.

An example with this code in a console application:

...

When we launched the application we can see how the timestamp, shows a small difference between the connected sensor and active sensor (lines 2y3 vs 4.5 lines)

   1: 20:23:32.9637224 - Start sample application
   2: 20:23:40.0351074 - New Status: Initializing
   3: 20:23:40.0351074 - Sensor Status: Initializing
   4: 20:23:44.7783652 - New Status: Connected
   5: 20:23:44.7783652 - Sensor Status: Connected
   6: 20:23:51.2279666 - New Status: Disconnected
   7: 20:23:51.2279666 - Sensor Status: Disconnected
   8: 20:23:59.2548372 - New Status: Initializing
   9: 20:23:59.2558370 - Sensor Status: Initializing
  10: 20:24:03.5861851 - New Status: Connected
  11: 20:24:03.5861851 - Sensor Status: Connected
  12: 20:24:08.8158976 - New Status: Disconnected
  13: 20:24:08.8158976 - Sensor Status: Disconnected

If we also want to take advantage of this event to initialize the sensor, we must bear in mind that we can only do so when the State of the sensor is "connected".

The complete example below shows one way of doing it.

Project Information URL: http://elbruno.com/2012/03/06/kinectsdk-howto-detect-the-sensor-state-change-and-sensor-disconnection/

using System; 
using Microsoft.Kinect;

namespace KinectStatus01 
{ 
    class Program 
    { 
        private static KinectSensor _kinect; 
        static void Main(string[] args) 
         { 
             Console.WriteLine("{0} - Start sample application", DateTime.Now.TimeOfDay.ToString()); 
             KinectSensor.KinectSensors.StatusChanged += KinectSensorsStatusChanged; 
             Console.ReadLine(); 
             if(_kinect != null) 
             { 
                 _kinect.Stop(); 
             } 
         } 
  

         static void KinectSensorsStatusChanged(object sender, StatusChangedEventArgs e) 
         { 
             Console.WriteLine("{0} - New Status: {1}", DateTime.Now.TimeOfDay.ToString(), e.Status); 
             _kinect = e.Sensor; 
             if (e.Sensor != null) 
             { 
                 Console.WriteLine("{0} - Sensor Status: {1}", DateTime.Now.TimeOfDay.ToString(), _kinect.Status); 
                 if (_kinect.Status == KinectStatus.Connected) 
                 { 
                     _kinect.DepthStream.Enable(); 
                     _kinect.ColorStream.Enable(); 
                     _kinect.SkeletonStream.Enable(); 
                     _kinect.Start(); 
                     Console.WriteLine("{0} - Sensor Started", DateTime.Now.TimeOfDay.ToString()); 
                 } 
             } 
             else 
             { 
                 Console.WriteLine("{0} - No Sensor", DateTime.Now.TimeOfDay.ToString()); 
             } 
         } 
     } 
}

 

Contact Information: