可以利用下面这些变成元素创建一个查询来搜索一定范围内的远程蓝牙设备:
注意 为了使清晰起见,文中忽略了错误处理
搜索并返回远程设备的地址
- 提供Winsock的版本和实现细节的数据来初始化caller application。可以通过调用WSAStartup函数来获得这个数据。
WSADATA wsd;
WSAStartup (MAKEWORD(1,0), &wsd);
- 创建并初始化一个WSAQUERYSET变量用于指定搜索参数,设置dwNameSpace成员为NS_BTH限制为查询蓝牙设备。
WSAQUERYSET wsaq;
ZeroMemory(&wsaq, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;
- 调用WSALookupServiceBegin函数来执行一个查询。
int iRet = WSALookupServiceBegin (&wsaq, LUP_CONTAINERS, &hLookup);
将LUP_CONTAINERS赋给dwFlags参数,启动SDP来搜索蓝牙设备。
注意 将dwFlags参数设为零将执行一个服务搜索。
WSALookupServiceBegin函数返回一个句柄到hLookup参数中。
- 要枚举在上一步中调用WSALookupServiceBegin所找到的设备,就要使用WSALookupServiceNex函数。函数返回一个指向存有查询结果的WSAQUERYSET及构体。
- 设置一个WSAQUERYSET结构体来储存WSALookupServiceNext函数返回的设备数据。
CHAR buf[4096];
LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;
- 调用WSALookupServiceNext并将WSALookupServiceBegin返回的hLookUp参数传递给它。为了提高性能,对WSALookupServiceBegin 的调用只返回设备的地址并存储在内存中。要返回设备的名称和地址,就要将LUP_RETURN_NAME | LUP_RETURN_ADDR赋给dwFlags参数。
DWORD dwSize = sizeof(buf);
int iRet = WSALookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults)
可以通过循环调用WSALookupServiceNext来枚举所有的设备。
- 调用WSALookupServiceEnd函数来结束设备搜索。这个函数将释放由WSALookupServiceBegin创建的lookup句柄。
WSALookupServiceEnd(hLookup);
- 要结束对Winsock服务的使用,调用WSACleanup函数。在程序中对每个成功调用的WSAStartup都必须对应地调用WSACleanup。
The following code example performs a device inquiry displays the device name in a user interface.
下面的代码执行了一次设备查询,并将设备名称显示在用户界面上。
//------------------------------------------------------------------------
// Function: PerformInquiry
// Purpose: Performs a device inquiry displays the device name in a user interface.
//------------------------------------------------------------------------
#define MAX_NAME 248
static BOOL PerformInquiry()
{
WSAQUERYSET wsaq;
HANDLE hLookup;
union {
CHAR buf[5000];
double __unused; // ensure proper alignment
};
LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
DWORD dwSize = sizeof(buf);
BOOL bHaveName;
ZeroMemory(&wsaq, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;
if (ERROR_SUCCESS != WSALookupServiceBegin (&wsaq, LUP_CONTAINERS, &hLookup))
{
wprintf(L"WSALookupServiceBegin failed %d\r\n", GetLastError());
return FALSE;
}
ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;
while (ERROR_SUCCESS == WSALookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults))
{
ASSERT (pwsaResults->dwNumberOfCsAddrs == 1);
BT_ADDR b = ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;
bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);
wprintf (L"%s%s%04x%08x%s\n", bHaveName ? pwsaResults->lpszServiceInstanceName : L"",
bHaveName ? L"(" : L"", GET_NAP(b), GET_SAP(b), bHaveName ? L")" : L"");
}
WSALookupServiceEnd(hLookup);
return TRUE;
}