【问题标题】:Getting Connection-State from Bluetooth Low Energy Device from Device Manager从设备管理器获取低功耗蓝牙设备的连接状态
【发布时间】:2016-03-09 08:49:47
【问题描述】:

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

到目前为止我尝试过的:

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

现在我想获得那个 Connected-Attribute,但我没有找到我必须在 SetupDiGetDeviceRegistryProperty 使用什么。

此处描述了SetupDiGetDeviceRegistryProperty https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx

也许有人知道什么是 Property 的正确值。

我的代码:

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

【问题讨论】:

    标签: c++ windows bluetooth-lowenergy


    【解决方案1】:

    您可以尝试以下方法,而不是使用 SetupDiEnumDeviceInfo: 1.使用SetupDiEnumDeviceInterfaces 2.使用SetupDiGetDeviceInterfaceProperty 3.使用SetupDiGetDeviceInterfacePropertyKeys获取接口可用的所有Property Keys的列表 4. 使用 SetupDiGetDeviceProperty 和/或 SetupDiGetDeviceRegistryProperty

    您可以使用 DEVPROP,而不是使用 SPDRP_XXX 常量,如 'devpkey.h' 中所定义... 下面是从我写的一个测试程序的日志中摘录的几个例子,以发现整个事情:

        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\8&2fd07168&1&0001
    BTHLEDevice\{00001801-0000-1000-8000-00805f9b34fb}_c026df001017\8&2fd07168&1&0008
    BTHLEDevice\{00001809-0000-1000-8000-00805f9b34fb}_c026df001017\8&2fd07168&1&000c
    BTHLEDevice\{0000180f-0000-1000-8000-00805f9b34fb}_c026df001017\8&2fd07168&1&0010
    BTHLEDevice\{0000180a-0000-1000-8000-00805f9b34fb}_c026df001017\8&2fd07168&1&0014
    BTHLEDevice\{00001523-1212-efde-1523-785feabcd123}_c026df001017\8&2fd07168&1&0019
    

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

    现在,为了回答您的问题,我仍在寻找自己 :) 但我不太确定: 1)知道连接状态是工作(动态)信息 2) 它是您可以通过上述方法访问的“属性”

    【讨论】:

      【解决方案2】:

      我找到了解决办法。

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

      希望这个 sn-p 有所帮助。

      【讨论】:

        猜你喜欢
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 2018-05-21
        相关资源
        最近更新 更多