I am trying to discover nearby bluetooth devices with the winsocket API using the bluetooth namespace.
Everything seem to work, but my call to WSALookupServiceNext() does only return one result, after that I get the WSA_E_NO_MORE error from WSAGetLastError().
One may say well, then there is only one device in range, but I can deny that, since there are several devices around my computer.
Here is some code I am using to discover devices:
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);
I always only get one line containing "Device found:XXXXXXXXXXXXXXXX", but I would like to query any device in range (first I don't care if remembered devices are included, but later on I only want currently in-range devices to operate on).
I appreciate any help.
Thanks, cya.