C ++蓝牙设备查询 - 只有一个结果 - Winsocket API

时间:2017-06-04 13:47:27

标签: c++ bluetooth winsock

我正在尝试使用bluetooth命名空间使用winsocket API发现附近的蓝牙设备。

一切似乎都有效,但是我对WSALookupServiceNext()的调用只返回一个结果,之后我从WSAGetLastError()中得到了WSA_E_NO_MORE错误。

有人可能会说,那么范围内只有一台设备,但我可以否认,因为我的电脑周围有几台设备。

以下是我用来发现设备的一些代码:

int iResult = WSA_SUCCESS;
ULONG ulFlags = LUP_CONTAINERS | LUP_FLUSHCACHE | LUP_RETURN_ADDR | LUP_RETURN_NAME, pwqsSize = 4096;
HANDLE hLookup = NULL;
PWSAQUERYSET pWSAQUerySet = NULL;
WSAQUERYSET wsaq;
WSADATA wsaData = { 0 };
SOCKADDR_BTH bthAddr = { 0 };

cout << "Starting wsa service" << endl;

iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

if (iResult != WSA_SUCCESS)
{
    cout << "Failed setting up wsa service" << endl;
    return 1;
}


cout << "Setting up bluetooth adapter to query available devices" << endl;

ZeroMemory(&wsaq, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;

pWSAQUerySet = (PWSAQUERYSET)malloc(pwqsSize);

if (pWSAQUerySet == NULL)
{
    cout << "Failed allocating memory." << endl;
    return 1;
}

ZeroMemory(pWSAQUerySet, pwqsSize);
pWSAQUerySet->dwNameSpace = NS_BTH;
pWSAQUerySet->dwSize = sizeof(WSAQUERYSET);
pWSAQUerySet->lpcsaBuffer = NULL;

cout << "Starting lookup service" << endl;

iResult = WSALookupServiceBegin(&wsaq, ulFlags, &hLookup);

if (iResult != WSA_SUCCESS) {
    cout << "Failed starting service!" << endl;
    cout << WSAGetLastError();
    return 1;
}

ZeroMemory(pWSAQUerySet, sizeof(WSAQUERYSET));
pWSAQUerySet->dwNameSpace = NS_BTH;
pWSAQUerySet->dwSize = sizeof(WSAQUERYSET);
pWSAQUerySet->lpBlob = NULL;

while (WSALookupServiceNext(hLookup, ulFlags, &pwqsSize, pWSAQUerySet) == ERROR_SUCCESS)
{
    cout << "Device found: " << pWSAQUerySet->lpszServiceInstanceName << endl;

}

WSALookupServiceEnd(hLookup);

我总是只得到一行包含&#34;设备找到:XXXXXXXXXXXXXXXX&#34;但我想查询范围内的任何设备(首先我不在乎是否包含记忆设备,但稍后我只想要当前的范围内设备进行操作。)

我感谢任何帮助。

谢谢,cya。

0 个答案:

没有答案