簡體   English   中英

從設備管理器中獲取低功耗藍牙設備的連接狀態

[英]Getting Connection-State from Bluetooth Low Energy Device from Device Manager

我正在開發藍牙低功耗設備,我需要在代碼中查看設備是否已連接。 我注意到的第一件事是 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;
}

不使用 SetupDiEnumDeviceInfo,您可以嘗試: 1. 使用 SetupDiEnumDeviceInterfaces 2. 使用 SetupDiGetDeviceInterfaceProperty 3. 使用 SetupDiGetDeviceInterfacePropertyKeys 獲取可用於接口的所有屬性鍵的列表 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\\ 樹上工作]。列出該設備的所有 GattServices 並獲取它們的 uuid 后,您可以在 device_guid 本身上重新啟動該迭代 SetupDiGetClassDevs(&GattServiceGUID...) [然后在注冊表中的 \\BTHLEDevice\\ 樹上工作]。

現在,為了回答您的問題,我仍在尋找自己 :) 但我不確定:1)它是了解連接狀態的有效(動態)信息 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"

        }

希望這個片段有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM