0
votes

I'm currently creating a program in C++ that allows devices (currently just my smartphone) to be paired to the PC. I'm using the WinApi functions and it actually works rather well.

For the programm to work I currently need to pass the Bluetooth MAC Address of the device to the programm, more specifically, the BluetoothRegisterForAuthenticationEx MSDN function needs it to pair the device.

Now, I actually don't want to type in my Bluetooth MAC Address, but it would be fine to enter the Device Name (or something else) in order to pair the device.

I looked through the winapi bluetooth functions, but didn't find anything, so my question for you is,

Can I, programmatically, get the Bluetooth MAC Address of a certain device when I have other informations (for example the device name) without being already paired? And if so, how?

2

2 Answers

0
votes

You can start a device discovery using WSALookupServiceBegin and WSALookupServiceNext, then for each device detected (each WSAQUERYSET) compare the lpszServiceInstanceNamewith the name typed by the user. If it matches, then you have the mac address in the lpcsaBuffer->RemoteAddr.lpSockaddr field . This field can be cast to PSOCKADDR_BTH, then you get the MAC addr in PSOCKADDR_BTH->btAddr

WSAQUERYSET querySet;
memset(&querySet, 0, sizeof(querySet));
querySet.dwSize = sizeof(querySet);
querySet.dwNameSpace = NS_BTH;
HANDLE hLookup;
if(0 != WSALookupServiceBegin(&querySet, LUP_CONTAINERS | LUP_FLUSHCACHE, &hLookup))
{
    if(WSAGetLastError() != WSASERVICE_NOT_FOUND)
    {
        // error during WSALookupServiceBegin
    }
    else
    {
        //No BlueTooth device Found
    }
    return res;
}
DWORD deviceLength = 2000;
char buf[deviceLength];
WSAQUERYSET* pDevice = PWSAQUERYSET(buf);
while (0 == WSALookupServiceNext(hLookup, LUP_RETURN_ADDR | LUP_RETURN_NAME, &deviceLength, pDevice))
{
    PSOCKADDR_BTH sa = PSOCKADDR_BTH(pDevice->lpcsaBuffer->RemoteAddr.lpSockaddr);
    if(sa->addressFamily != AF_BTH)
    {
       // Address family is not AF_BTH  for bluetooth device discovered
        continue;
    }
    //the name is available in pDevice->lpszServiceInstanceName
    //the MAC address is available in sa->btAddr
}
WSALookupServiceEnd(hLookup);
0
votes

I found the exact method I was searching for in the Windows SDK samples.

Microsoft SDKs\Windows\v7.0\Samples\netds\winsock\bluetooth

The mehtod is called NameToBthAddr and does pretty much what Eric Lemanissier was suggesting.