简体   繁体   English

如何连接蓝牙低功耗设备

[英]How to connect to the bluetooth low energy device

I am writing program for Win 8 tablet.我正在为 Win 8 平板电脑编写程序。 I need to connect an external BLE device.我需要连接外部 BLE 设备。 The device is already paired with Windows and I can see it in Device Manager.该设备已与 Windows 配对,我可以在设备管理器中看到它。 But I can not figure out how to connect it.但我不知道如何连接它。

With SetupDiEnumDeviceInfo and SetupDiGetDeviceProperty I can get some information about the BLE-device, but to perform, eg BluetoothGATTGetServices Handle device requires.使用SetupDiEnumDeviceInfoSetupDiGetDeviceProperty我可以获得有关 BLE 设备的一些信息,但要执行,例如BluetoothGATTGetServices Handle 设备需要。 I do not know where to take it.我不知道该去哪里。 Perhaps i can use CreateFile , but it is not clear that the substitute as the first argument lpFileName.也许我可以使用CreateFile ,但不清楚将替代作为第一个参数 lpFileName。

Here's a piece of code with which I'm looking for my device.这是我正在寻找我的设备的一段代码。

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;

I moved a little further, but still i can't get the data from device.我移动了一点,但我仍然无法从设备中获取数据。

  1. To obtain "Device Interface Path" had to use the other functions: SetupDiGetClassDevs , SetupDiEnumDeviceInterfaces and SetupDiGetDeviceInterfaceDetail .要获得“设备接口路径”,必须使用其他函数: SetupDiGetClassDevsSetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail

  2. Next, with CreateFile I get HANDLE BLE-device.接下来,使用CreateFile我得到 HANDLE BLE-device。

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

  3. Next using WinAPI BluetoothGATTGetServices and BluetoothGATTGetCharacteristics I get the appropriate structures.接下来使用 WinAPI BluetoothGATTGetServicesBluetoothGATTGetCharacteristics我得到了适当的结构。

But when trying to get the property value with BluetoothGATTGetCharacteristicsValue , I get ERROR_ACCESS_DENIED .但是当尝试使用BluetoothGATTGetCharacteristicsValue获取属性值时,我得到ERROR_ACCESS_DENIED

And then I do not know what to do.然后我不知道该怎么办。 What could be wrong?有什么问题?

Andrey, I think the problem is that your device is not connected and BluetoothGATTGetCharacteristicsValue is not triggering a connection.安德烈,我认为问题在于您的设备未连接并且BluetoothGATTGetCharacteristicsValue未触发连接。

Try manually to connect your device using Windows tools.尝试使用 Windows 工具手动连接您的设备。 I've the following flow that helps me: Unpair device, Pair device -> It should appear as connected ( it worked in my case ;) )我有以下对我有帮助的流程:取消配对设备,配对设备->它应该显示为已连接(它在我的情况下有效;))

Anyway, If this is not helping, try to run "As Administrator", this helps in some cases.无论如何,如果这没有帮助,请尝试运行“以管理员身份”运行,这在某些情况下会有所帮助。

Good luck!!!祝你好运!!!

Note: Would be very interested to know how to retrievethe device path for the BTLE device in order to call BluetoothGATTGetServices?注意:想知道如何检索 BTLE 设备的设备路径以调用 BluetoothGATTGetServices?

gattServiceGUID is any long form BLE UUID supported by your device. 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 
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM