首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何连接蓝牙低功耗设备

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

Stack Overflow用户
提问于 2013-11-06 17:45:12
回答 1查看 7.1K关注 0票数 6

我正在为Win 8平板电脑写程序。我需要连接一个外部BLE设备。该设备已与Windows配对,我可以在设备管理器中看到它。但我想不出怎么连接它。

使用SetupDiEnumDeviceInfoSetupDiGetDeviceProperty,我可以获得有关BLE设备的一些信息,但要执行,例如BluetoothGATTGetServices Handle device requires。我不知道去哪里买。也许我可以使用CreateFile,但不清楚作为第一个参数的替代参数是lpFileName。

这是一段代码,我用它来查找我的设备。

代码语言:javascript
复制
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;

我走得更远了一点,但我仍然无法从设备上获取数据。

要获得“设备接口路径”,必须使用其他函数:SetupDiGetClassDevsSetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail.

  • Next,与CreateFile我得到处理BLE-

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

  • 下一步使用WinAPI DevicePath和

获取适当的结构。

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

然后我就不知道该怎么办了。会出什么问题呢?

EN

回答 1

Stack Overflow用户

发布于 2013-12-12 08:34:25

注意:我很想知道如何检索BTLE设备的设备路径以便调用BluetoothGATTGetServices?

gattServiceGUID是您的设备支持的任何长格式的BLE。

代码语言:javascript
复制
"{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 
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19808624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档