如何检测程序运行时何时断开/拔出kinect?

时间:2012-10-02 13:27:32

标签: c++ visual-c++ kinect

我正在使用其中一个官方Kinect SDK 1.5示例,我正在尝试弄清楚如何添加检查来检测Kinect何时断开连接。目前该应用程序将冻结,因此必须有办法防止这种情况发生。

这是SDK示例中的主要消息循环:

// 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);

我添加了以下内容:

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

因为我假设NuiStatus可能是一种检测断开连接的方法。不幸的是它不起作用。检查m_pNuiSensorNULL是否也是如此。

那么在正在运行的应用程序中检测断开连接的方法是什么?

EDIT1:我应该使用NuiSetDeviceStatusCallback吗?

2 个答案:

答案 0 :(得分:2)

documentation中,它说NuiStatus返回HRESULT而不是bool,所以不应该

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

代替?

答案 1 :(得分:0)

以下解决方案有效。

    // check if m_pNuiSensor is initialized.
    if (NULL != m_pNuiSensor)
    {
        // get current status & check if not ok.
        HRESULT current_status = m_pNuiSensor->NuiStatus();
        if (current_status != S_OK )
        {
            SetStatusMessage(L"Lost connection to Kinect!");
        }
    }