Reputation: 167
I have to write an application which has to identify "ESP32" devices & send/receive data from a Windows C++ application.
Q1: I am using WSALookupServiceBegin()
API to find the BT device, it was not working as expected. API returns 10108 until I manually click on "Add devices" in Windows Bluetooth window. Is there any other API/service which can discover BT near devices or am I using the WSALookupServiceBegin()
API wrongly?
Is WSALookupServiceBegin()
takes the device data from the Bluetooth cache? I got this doubt because API works fine only after manual search in Windows.
Q2: is it possible to connect to any Bluetooth device just with the Mac ID of BT device from Windows?
Please find the code below.
WSAQUERYSET data;
HANDLE handle;
ZeroMemory(&data, sizeof(data));
data.dwSize = sizeof(data);
data.dwNameSpace = NS_BTH;
data.lpcsaBuffer = NULL;
WSALookupServiceBegin(&data, LUP_CONTAINERS, &handle);
while(WSALookupServiceNext(hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR,
&dwSize, pwsaResults)
{
service_classID = pwsaResults->lpServiceClassId;
_BTH_DEVICE_INFO *dev = (_BTH_DEVICE_INFO *)pwsaResults->lpBlob->pBlobData;
SOCKET LocalSocket = INVALID_SOCKET;
SOCKADDR_BTH SockAddrBthServer;
SockAddrBthServer.btAddr = dev->address;
SockAddrBthServer.addressFamily = AF_BTH;
SockAddrBthServer.serviceClassId = *service_classID;
SockAddrBthServer.port = 0;
// connect to socket
LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (INVALID_SOCKET == LocalSocket) {
wprintf(L"socket() call failed. WSAGetLastError = [%d]\n", WSAGetLastError());
return -1;
}
if (SOCKET_ERROR == connect(LocalSocket,
(struct sockaddr *) &SockAddrBthServer,
sizeof(SOCKADDR_BTH))) {
wprintf(L"connect() call failed. WSAGetLastError=[%d]\n", WSAGetLastError());
return -1;
}
}
OUTPUT :
connect() call failed. WSAGetLastError=[10049]
Upvotes: 0
Views: 473
Reputation: 1024
WSALookupServiceBegin
is correct way but you have to provide correct flags for it. Also you can use BluetoothFidnFirstDevice
and BluetoothFindNextDevice
functions from Bluetooth API.
However both methods always return paired devices even they are not available (together with just found devices).
From your description it looks you did not provide correct flags for WSAxxx
function.
If you know device's MAC and it has not been changed then you can connect to device by MAC without rediscovering it each time. Depending on your device's authentication requirement you even do not need to pair with device (of course if your device does not need authentication and/or encryption).
Upvotes: 1