c++ - 如何连接到蓝牙低功耗设备

标签 c++ windows-8 bluetooth-lowenergy

我正在为 Win 8 平板电脑编写程序。我需要连接外部 BLE 设备。 该设备已与 Windows 配对,我可以在设备管理器中看到它。但我不知道如何连接它。

使用 SetupDiEnumDeviceInfoSetupDiGetDeviceProperty 我可以获得一些关于 BLE 设备的信息,但是要执行,例如BluetoothGATTGetServices handle 设备要求。我不知道把它带到哪里去。或许我可以使用CreateFile,但不清楚第一个参数 lpFileName 的替代品。

这是我正在寻找我的设备的一段代码。

HDEVINFO hDevInfo;
   SP_DEVINFO_DATA DeviceInfoData;
   DWORD i;

   // Create a HDEVINFO with all present devices.
   hDevInfo = SetupDiGetClassDevs(
        &BluetoothClassGUID,                     /* GUID_DEVCLASS_BLUETOOTH */
        0, 0, 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;

       while (!SetupDiGetDeviceRegistryProperty(
               hDevInfo,
               &DeviceInfoData,
               SPDRP_FRIENDLYNAME,
               &DataT,
               (PBYTE)buffer,
               buffersize,
               &buffersize))
       {
           if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
               // Change the buffer size.
               if (buffer) delete(buffer);
               // Double the size to avoid problems on
               // W2k MBCS systems per KB 888609.
               buffer = new wchar_t[buffersize * 2];
           }else{
               // Insert error handling here.
               break;
           }
       }
                   /* Here i just compare by name is this my device or not */
                   ...
                   /* Here i just compare by name is this my device or not */
        if (buffer) delete(buffer);
   }


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

   //  Cleanup
   SetupDiDestroyDeviceInfoList(hDevInfo);

   return;// 0;

我移动了一点,但我仍然无法从设备获取数据。

  1. 要获取“设备接口(interface)路径”必须使用其他函数: SetupDiGetClassDevsSetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail

  2. 接下来,使用 CreateFile 我得到 HANDLE BLE 设备。

    hComm = CreateFile(pInterfaceDetailData->DevicePath, GENERIC_WRITE | GENERIC_READ,NULL,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);

  3. 接下来使用 WinAPI BluetoothGATTGetServicesBluetoothGATTGetCharacteristics 我得到了适当的结构。

但是当尝试使用 BluetoothGATTGetCharacteristicsValue 获取属性值时,我得到了 ERROR_ACCESS_DENIED

然后我不知道该怎么办。有什么问题吗?

最佳答案

Andrey,我认为问题在于您的设备未连接并且 BluetoothGATTGetCharacteristicsValue 未触发连接。

尝试使用 Windows 工具手动连接您的设备。我有以下流程可以帮助我:取消配对设备,配对设备 ->它应该显示为已连接(它在我的情况下有效;))

无论如何,如果这没有帮助,请尝试以“管理员身份”运行,这在某些情况下会有所帮助。

祝你好运!!!

关于c++ - 如何连接到蓝牙低功耗设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19808624/

相关文章:

windows - 蓝牙低功耗 API Windows 8

c++ - 使用 constexpr 验证构造函数中的文字参数

windows - Windows 8 Metro 应用程序是否需要对代码进行任何更改才能在基于 ARM 的设备上运行?

c++ - 有没有办法通过逆向工程来恢复结构。指向该结构的指针由 DLL 中唯一用于导出的函数返回

c# - 如何打开新页面?

c# - 播放背景音乐地铁应用

c - Linux:从蓝牙 LE 接收数据

javascript - 使用react-native-ble-plx包发送数据

c++ - 重载强制转换以使类的行为与指针相同时的正确行为

c++ - 从字节缓冲区中提取 IP