主要使用以下接口:
BthSetSecurityUI 設置收到配對請求後,可以收到消息的事件;設置超時時間
BthGetPINRequest 獲得與該設備請求配對的設備地址
BthSetPIN/BthRefusePINRequest 設置PIN/拒絕對方的配對請求
主要是BthSetSecurityUI 的使用:
第一個參數爲事件,當藍牙設備收到其他設備的配對請求,此事件有信號;第二個參數爲超時時間,當第一個參數事件變爲有信號時,需要在這個時間範圍內調用BthGetPINRequest才能獲得對方設備的地址;第三個參數也爲超時時間,只有在這個時間內調用BthSetPIN設置PIN碼,才OK
附加代碼,主要是通過線程來監測其他設備的配對請求
#define ADIO_BTSERVICE_STORE_TO 3000
#define ADIO_BTSERVICE_USER_TO 60000
#define ADIO_BTSERVICE_SLEEP 5000
DWORD WINAPI ADIOCheckPairRequestThread(LPVOID lpvoid)
{
int iRetVal = ADIO_RESULT_FAILED;
BT_ADDR ullAddr = 0x00;
HANDLE hWaitEvent[2] = {0};
HANDLE hAuthReq = NULL;
int iRet = ERROR_SUCCESS;
BOOL bThreadRun = TRUE;
DWORD dwWaitRet = 0;
while ( bThreadRun )
{
hAuthReq = CreateEvent(NULL, FALSE, FALSE, NULL);
if ( hAuthReq != NULL )
{
/* Provide an event that will be signaled by the stack when a device without a PIN sends a PIN request */
iRet = BthSetSecurityUI(hAuthReq, ADIO_BTSERVICE_STORE_TO, ADIO_BTSERVICE_USER_TO);
if ( iRet == ERROR_SUCCESS )
{
hWaitEvent[0] = g_hThreadExitEvent;
hWaitEvent[1] = hAuthReq;
/* Wait two thread:exit thread and pin request */
dwWaitRet = WaitForMultipleObjects (2, hWaitEvent, FALSE, INFINITE);
if ( dwWaitRet == (WAIT_OBJECT_0 + 1) )
{
/* Wait pin request event */
while ( BthGetPINRequest(&ullAddr) == ERROR_SUCCESS )
{
/* Put the address of peer device into list */
ADIODealPairReqFunc(ullAddr);
}
}
else
{
/* Wait exit thread event */
bThreadRun = FALSE;
}
}
else if ( iRet == ERROR_SERVICE_NOT_ACTIVE )
{
dwWaitRet = WaitForSingleObject(g_hThreadExitEvent, ADIO_BTSERVICE_SLEEP);
if ( dwWaitRet != WAIT_TIMEOUT )
{
/* Wait exit thread event */
bThreadRun = FALSE;
}
}
else
{
bThreadRun = FALSE;
}
BthSetSecurityUI (NULL, 0, 0);
CloseHandle(hAuthReq);
hAuthReq = NULL;
}
}
return iRetVal;
}