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

6
我正在为Win 8平板编写程序。我需要连接一个外部BLE设备。 该设备已经与Windows配对,我可以在设备管理器中看到它。但我不知道如何连接它。
使用SetupDiEnumDeviceInfoSetupDiGetDeviceProperty,我可以获取有关BLE设备的一些信息,但要执行例如BluetoothGATTGetServices 需要句柄设备。我不知道从哪里获取它。也许我可以使用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. 为了获取“设备接口路径”,必须使用其他函数:SetupDiGetClassDevsSetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail

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

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

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

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

然后我不知道该怎么办。可能出了什么问题?


那方面有进展了吗? - Pupsik
你使用了哪个接口UUID来查找你的设备?GUID_BTHPORT_DEVICE_INTERFACE只返回了内部BLE扫描器。 - Myon
2个回答

0

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

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

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

祝你好运!


0
注意:非常想知道如何检索BTLE设备的设备路径,以便调用BluetoothGATTGetServices?
gattServiceGUID是您的设备支持的任何长形式BLE UUID。
"{00001803-0000-1000-8000-00805F9B34FB"} can be used to open a handle to the Link Loss service if supported by the device you are trying to open

HDEVINFO hDevInfo = SetupDiGetClassDevs(&gattServiceGUID, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

if (hDevInfo != INVALID_HANDLE_VALUE)
{
    SP_DEVICE_INTERFACE_DATA interfaceData;
    ZeroMemory(&interfaceData,sizeof(SP_DEVICE_INTERFACE_DATA));
    interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    for (DWORD dwDeviceIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &gattServiceGUID, dwDeviceIndex, &interfaceData); dwDeviceIndex++)
    {                       
        dwDeviceCount++;
        SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &dwBytesNeeded, NULL);
        if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            pInterfaceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) new byte[dwBytesNeeded];

            SP_DEVINFO_DATA spDeviceInfoData = { sizeof(SP_DEVINFO_DATA) };

            ZeroMemory(pInterfaceDetail, sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA));
            pInterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

            //  grab the interface detail
            if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pInterfaceDetail, dwBytesNeeded, NULL, &spDeviceInfoData) == TRUE)
            {
                //  request a handle to the GATT service path
                m_hGattServiceHandle = CreateFile(pInterfaceDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
                if (m_hGattServiceHandle != INVALID_HANDLE_VALUE)
                {
                    now you can drill down the characteristics and descriptors with the m_hGattServiceHandle 
                }
            }
        }
    }
}

1
你可能想要添加一些解释。目前它只是一段代码块。 - Josh Crozier
我该如何深入了解?我仍需要设备句柄来执行诸如“BluetoothGATTSetCharacteristicValue”之类的函数。 - Myon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接