从设备管理器获取蓝牙低功耗设备的连接状态

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

我正在开发低功耗蓝牙设备,我需要在代码中查看设备是否已连接。 我注意到的第一件事是 Devicemanager 中有一个属性 "Verbunden"-> English: Connected 如果我的设备已连接或未连接,它会显示 true 或 false。所以我需要在我的程序中读取该属性。

到目前为止我尝试过的:

使用 SetupDiGetClassDevs 获取所有设备 使用 SetupDiGetDeviceRegistry 获取 FriendlyName属性 使用名称搜索我的设备。 行得通。

现在我想获得连接属性,但我没有在 SetupDiGetDeviceRegistry属性.

中找到必须使用的内容

SetupDiGetDeviceRegistry属性 在这里描述 https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx

也许有人知道 属性 的正确值是多少。

我的代码:

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;
}

不使用 SetupDiEnumDeviceInfo,您可以尝试: 1. 使用 SetupDiEnumDeviceInterfaces 2. 使用 SetupDiGetDeviceInterface属性 3. 使用 SetupDiGetDeviceInterface属性Keys 获取接口可用的所有 属性 键的列表 4. 使用 SetupDiGetDevice属性 and/or SetupDiGetDeviceRegistry属性

不使用 SPDRP_XXX 常量,而是使用 'devpkey.h' 中定义的 DEVPROP ... 下面是我为发现整个事情而写的测试程序日志中的一些示例:

    DEVPROPNAME: DEVPKEY_DeviceInterface_Bluetooth_DeviceAddress
    DEVPROPGUID: {2BD67D8B-8BEB-48D5-87E0-6CDA3428040A}
    DEVPROPPID: 1
    DEVPROPTYPE: DEVPROP_TYPE_STRING
    Value: c026df001017
   DEVPROPNAME: DEVPKEY_Device_Children
   DEVPROPGUID: {4340A6C5-93FA-4706-972C-7B648008A5A7}
   DEVPROPPID: 9
   DEVPROPTYPE: DEVPROP_TYPE_STRING_LIST
   Value:
BTHLEDevice\{00001800-0000-1000-8000-00805f9b34fb}_c026df001017&2fd07168&1&0001
BTHLEDevice\{00001801-0000-1000-8000-00805f9b34fb}_c026df001017&2fd07168&1&0008
BTHLEDevice\{00001809-0000-1000-8000-00805f9b34fb}_c026df001017&2fd07168&1&000c
BTHLEDevice\{0000180f-0000-1000-8000-00805f9b34fb}_c026df001017&2fd07168&1&0010
BTHLEDevice\{0000180a-0000-1000-8000-00805f9b34fb}_c026df001017&2fd07168&1&0014
BTHLEDevice\{00001523-1212-efde-1523-785feabcd123}_c026df001017&2fd07168&1&0019

关于第二个主题,您 'working' 在 'device' 本身( SetupDiGetClassDevs(&BluetoothInterfaceGUID...) [然后在注册表中的 \BTHLE\ 树上工作]。 在列出该设备的所有 GattServices 并获取它们的 uuid 后,您可以在 device_guid 本身 SetupDiGetClassDevs(&GattServiceGUID...) 上重新启动该迭代 [然后在注册表中的 \BTHLEDevice\ 树上工作]。

现在,为了回答你的问题,我仍在寻找自己:)但我不太确定: 1)知道连接状态是一个工作(动态)信息 2) 是一个'Property'可以通过上面的方法访问

我找到了解决办法。

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"

        }

希望这段代码对您有所帮助。