如何连接蓝牙低功耗设备

And*_*rey 6 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;
Run Code Online (Sandbox Code Playgroud)

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

  1. 要获得"设备接口路径",必须使用其他功能: SetupDiGetClassDevs,SetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail.

  2. 接下来,使用CreateFile,我得到HANDLE BLE-device.

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

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

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

然后我不知道该怎么做.可能有什么不对?

Pup*_*sik 0

安德烈,我认为问题是您的设备未连接,并且BluetoothGATTGetCharacteristicsValue未触发连接。

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

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

祝你好运!!!