主要使用以下接口:
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;
}