Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect when kinect is disconnected / unplugged while program is running?

I'm working with one of the official Kinect SDK 1.5 examples, and I'm trying to figure out how to add a check to detect when the Kinect is being disconnected. Currently the app will just freeze, so there must be a way to prevent this from happening.

This is the main message loop from the SDK example:

// Main message loop
while (WM_QUIT != msg.message)
{
    hEvents[0] = m_hNextDepthFrameEvent;

    // Check to see if we have either a message (by passing in QS_ALLINPUT)
    // Or a Kinect event (hEvents)
    // Update() will check for Kinect events individually, in case more than one are signalled
    DWORD dwEvent = MsgWaitForMultipleObjects(eventCount, hEvents, FALSE, INFINITE, QS_ALLINPUT);

    // Check if this is an event we're waiting on and not a timeout or message
    if (WAIT_OBJECT_0 == dwEvent)
    {
        Update();
    }

    // does not work.
    bool bla = m_pNuiSensor->NuiStatus();
    if (NULL == m_pNuiSensor)
    {
            cout << 1 << endl;
    }

    if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
    {
        // If a dialog message will be taken care of by the dialog proc
        if ((hWndApp != NULL) && IsDialogMessageW(hWndApp, &msg))
        {
            continue;
        }

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
}

return static_cast<int>(msg.wParam);

I've added the following bit:

    // does not work, bla will always be the same value.
    bool bla = m_pNuiSensor->NuiStatus();
    if (NULL == m_pNuiSensor)
    {
            cout << 1 << endl;
    }

since I was assuming that maybe NuiStatus would be a way to detect the disconnect. Unfortunately it won't work. The same is true for checking whether m_pNuiSensor is NULL.

So what's the way to detect a disconnect in the running app?

EDIT1: should I be using NuiSetDeviceStatusCallback?

like image 940
memyself Avatar asked Dec 12 '25 19:12

memyself


1 Answers

In the documentation it says that NuiStatus returns HRESULT and not bool, so shouldn't it be

HRESULT bla = m_pNuiSensor->NuiStatus();
if (bla == E_NUI_NOTCONNECTED)
{
        cout << 1 << endl;
}

instead?

like image 146
Sassa Avatar answered Dec 15 '25 09:12

Sassa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!