none
BluetoothGATTRegisterEvent multiple devices RRS feed

  • Question

  • My code works fine in the case that I have a single device, when I add the second device, I am able to enumerate it and read the characteristics properly (i.e. device specific information like it's serial number are read correctly) but when I set up a CharacteristicValueChangedEvent the data that is sent to the callback for each device is the data from the first device.  

    Any insight on how to get notifications from multiple devices routed properly would be greatly appreciated

    Device handles are created like this 

      hDevInfo = SetupDiGetClassDevs( &ServiceIMU, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE );
       for ( DWORD i = 0; SetupDiEnumDeviceInfo( hDevInfo, i, &DeviceInfoData ); i++ )
       {
         std::wstring FriendlyName = GetStringProperty( hDevInfo, DeviceInfoData, SPDRP_FRIENDLYNAME );
         if ( FriendlyName == L"IMU" )
         {
           for ( DWORD j = 0; SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &ServiceIMU, j, &did ); ++j )
           {
            if ( !SetupDiGetDeviceInterfaceDetail( hDevInfo, &did, NULL, 0, &size, 0 ) )
             {
                PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)GlobalAlloc( GPTR, size );
                pInterfaceDetailData->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA );
                if ( !SetupDiGetDeviceInterfaceDetail( hDevInfo, &did, pInterfaceDetailData, size, &size, &dd ) )
                 break;
    
                 hLEDevice = CreateFile(
                   pInterfaceDetailData->DevicePath,
                   GENERIC_WRITE | GENERIC_READ,
                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                   NULL,
                   OPEN_EXISTING,
                   0,
                   NULL );
               

    and the event registration looks like this

     HRESULT hr = BluetoothGATTGetServices( hLEDevice, 0, NULL, &serviceBufferCount, BLUETOOTH_GATT_FLAG_NONE );
     PBTH_LE_GATT_SERVICE pServiceBuffer = (PBTH_LE_GATT_SERVICE)malloc( sizeof( BTH_LE_GATT_SERVICE ) * serviceBufferCount );
     if ( NULL == pServiceBuffer )  return false;
     else RtlZeroMemory( pServiceBuffer, sizeof( BTH_LE_GATT_SERVICE ) * serviceBufferCount );
    
      USHORT numServices;
      hr = BluetoothGATTGetServices( hLEDevice, serviceBufferCount, pServiceBuffer, &numServices, BLUETOOTH_GATT_FLAG_NONE );
    
      if ( S_OK != hr ) return false;
    
      USHORT charBufferSize;
      hr = BluetoothGATTGetCharacteristics( hLEDevice, pServiceBuffer, 0, NULL, &charBufferSize, BLUETOOTH_GATT_FLAG_NONE );
    
      if ( HRESULT_FROM_WIN32( ERROR_MORE_DATA ) != hr ) return false;
    
      PBTH_LE_GATT_CHARACTERISTIC pCharBuffer;
      if ( charBufferSize > 0 )
      {
        pCharBuffer = (PBTH_LE_GATT_CHARACTERISTIC)malloc( charBufferSize * sizeof( BTH_LE_GATT_CHARACTERISTIC ) );
    
        if ( NULL == pCharBuffer ) return false;
        else RtlZeroMemory( pCharBuffer, charBufferSize * sizeof( BTH_LE_GATT_CHARACTERISTIC ) );
    
        USHORT numChars;
        hr = BluetoothGATTGetCharacteristics( hLEDevice, pServiceBuffer, charBufferSize, pCharBuffer, &numChars, BLUETOOTH_GATT_FLAG_NONE );
    
        if ( S_OK != hr )
        {
          return false;
        }
        else
        {
          for ( int charIdx = 0; charIdx < numChars; ++charIdx )
          {
            PBTH_LE_GATT_CHARACTERISTIC pCharacteristic = &pCharBuffer[ charIdx ];
            BLUETOOTH_GATT_EVENT_HANDLE EventHandle;
            if ( pCharacteristic->IsNotifiable )
            {
              BTH_LE_GATT_EVENT_TYPE EventType = CharacteristicValueChangedEvent;
              BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION EventParameterIn;
              EventParameterIn.Characteristics[ 0 ] = *pCharacteristic;
              EventParameterIn.NumCharacteristics = 1;
    
              if ( pCharacteristic->CharacteristicUuid.Value.LongUuid.Data1 == uuidNewDataNotification )
              {
                hr = BluetoothGATTRegisterEvent(
                  hLEDevice,
                  EventType,
                  &EventParameterIn,
                  Callback,
                  NULL,
                  &EventHandle,
                  BLUETOOTH_GATT_FLAG_NONE );
              }
    
     

    Thursday, July 13, 2017 12:49 PM

All replies

  • Please submit a Feedback Hub report to reproduce the issue and submit Bluetooth logs:

    To submit feedback, please follow these steps:

    1.       Search for “Feedback Hub” in Cortana search, then launch the app.

    2.       Navigate to Feedback in the left menu, then press + Add new feedback.

    3.       Select the Problem button, share any details you think are relevant, and choose an appropriate category and subcategory. (For proper Bluetooth logs to be captured it must be one of the Bluetooth context paths, e.g. Hardware, Devices, and Drivers -> Bluetooth – Discovery, Pairing, and Connectivity)

    4.       Important: If possible, reproduce the problem(s) after clicking Begin monitoring (or Start capture) near the end of the form; Stop monitoring when you’re done.

    5.       Once you’ve completed the form and monitoring, click Submit.

    6.       Click Continue using Feedback Hub.

    7.       Click My feedback at the top, then find the item you submitted (it may take a while to appear).

    8.       Open the item you submitted, then click Share.

    9.       Click Copy link, then paste the link here in your response.  (It will look like https://aka.ms/<unique link>.)

    Thursday, July 13, 2017 8:36 PM
  • so of course I can not reproduce it while trying to get you a capture, if it begins to happen I again I will try this process

    thank you for the quick response!

    Friday, July 14, 2017 5:09 PM