KingKarl237 KingKarl237 - 29 days ago 20
C++ Question

Getting Connection-State from Bluetooth Low Energy Device from Device Manager

i'm developing on a Bluetooth Low Energy Device and i need to see in code if the device is connected or not.
First thing i noticed was that there is in the Devicemanager a Attribute "Verbunden"-> English: Connected and it says true or false if my device is connected or not. So i need to read that Attribute in my program.

What i have tried till now:

Getting all Devices with SetupDiGetClassDevs
Getting the FriendlyName with SetupDiGetDeviceRegistryProperty
Searching for my Device with the name.
That works.

Now i wanted to get that Connected-Attribute but i didn't find out what i have to use at SetupDiGetDeviceRegistryProperty.

SetupDiGetDeviceRegistryProperty is described here https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx

Maybe someone knows what is the right value for Property.

My Code:

int get_device_info( void )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
FILE * devices = fopen("devices.txt", "a+");
GUID AGuid;
//GUID can be constructed from "{xxx....}" string using CLSID
CLSIDFromString(TEXT(TO_SEARCH_DEVICE_UUID), &AGuid);
GUID BluetoothInterfaceGUID = AGuid;

// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(&BluetoothInterfaceGUID,
0, // Enumerator
0,
DIGCF_ALLCLASSES | DIGCF_PRESENT);

if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}

// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;

//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
//SPDRP_DEVICEDESC,
//SPDRP_CAPABILITIES,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = (wchar_t *)LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
if(buffer)
{

if( strcmp("Name of Device",AnsiString(buffer).c_str())==0)
{
fprintf(devices,"Result:[%s]",AnsiString(buffer).c_str());

if (buffer) LocalFree(buffer);
}
}

}


if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}

// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
fclose(devices);
return 0;
}

Answer

I have found out a solution.

GUID AGuid;
    //GUID can be constructed from "{xxx....}" string using CLSID
   CLSIDFromString(TEXT(TO_SEARCH_DEVICE_UUID), &AGuid);
   GUID BluetoothInterfaceGUID = AGuid;
   // Create a HDEVINFO with all present devices.
   hDevInfo = SetupDiGetClassDevs(&BluetoothInterfaceGUID,
       0, // Enumerator
       0,
       DIGCF_ALLCLASSES | DIGCF_PRESENT);//DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);//DIGCF_ALLCLASSES | DIGCF_PRESENT);

   if (hDevInfo == INVALID_HANDLE_VALUE)
   {
       // Insert error handling here.
       return 1;
   }


   // Enumerate through all devices in Set.

   DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
   for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
       &DeviceInfoData);i++)
   {
       DWORD DataT;
       LPTSTR buffer = NULL;
       LPTSTR buffer1 = NULL;
       DWORD buffersize = 0;

       while (!SetupDiGetDeviceRegistryProperty( // Get Name
           hDevInfo,
           &DeviceInfoData,
           SPDRP_FRIENDLYNAME,
           &DataT,
           (PBYTE)buffer,
           buffersize,
           &buffersize))
       {
           if (GetLastError() ==
               ERROR_INSUFFICIENT_BUFFER)
           {
               // Change the buffer size.
               if (buffer) LocalFree(buffer);
               // Double the size to avoid problems on
               // W2k MBCS systems per KB 888609.
               buffer = (wchar_t *)LocalAlloc(LPTR,buffersize * 2);
           }
           else
           {
               // Insert error handling here.
               break;
           }
       }
        {

       if(strcmp("Your Device",AnsiString(buffer).c_str())==0)  //Found your device
       {

        //########
       DEVPROPTYPE ulPropertyType;
       DWORD dwSize;
       ULONG devst;

//     memset(devst,0,sizeof(devst));
       bool err = SetupDiGetDeviceProperty(   //Checking Connection State
            hDevInfo,
            &DeviceInfoData,
            &DEVPKEY_Device_DevNodeStatus,   //Connected(0x02000000)
            &ulPropertyType,
            (BYTE *) &devst,
            sizeof(devst),
            &dwSize,
            0);
       DWORD error;
       error = GetLastError();



        if (devst &0x02000000) {
            //"Status: Getrennt "

        }
        else
        {
            //"Status: Verbunden"

        }

Hope this snippet helps.