2012-10-02 59 views
3

我正在使用官方的Kinect SDK 1.5示例之一,並試圖找出如何添加檢查以檢測Kinect何時斷開連接。目前該應用將會凍結,所以必須有一種方法來防止這種情況發生。如何檢測程序運行時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_pNuiSensor是否爲NULL也是如此。

那麼在正在運行的應用程序中檢測斷開連接的方式是什麼?

EDIT1:我應該使用NuiSetDeviceStatusCallback

+3

標題不應該是「如何檢測Kinect何時被忽略」? –

+0

@PeteBecker :)我改變了標題,使其更清晰。 – memyself

回答

2

documentation它說,NuiStatus返回HRESULT,而不是BOOL,所以它不應該是

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

呢?

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!"); 
     } 
    } 
相關問題