none
Disconnect Bluetooth Low Energy RRS feed

  • Question

  • Hello,

    I would like to know how i can disconnect from a device in c# using the "Windows.Devices.Bluetooth.GenericAttributeProfil" namespace.

    I first need to communicate with the device via this API, then i need to disconnect and communicate with the device via another software while the first software is still running. Communication with first software works fine but I don't know how to disconnect properly. The second software isn't able to find the device until i unplug the Bluetooth dongle from my computer. Second software is using another API where scanning for devices is possible.

    I tried to close connection by removing all ValueChanged EventHandler, setting all GattCharacteristic instances to null and disposing the GattDeviceService instance. I'm also waiting for about 60 seconds between first software finished communication and start of seconds software but it seems connection from first software is still established. As i said only after unplugging the Bluetooth dongle the second software can find the device.

    Best regards,

    Dominik


    Monday, July 27, 2015 11:22 AM

Answers

  • Hi Dominik,

    For Bluetooth LE devices our APIs do not provide direct control over the connection to the device. Instead our stack will disconnect the device after a one second timeout if there are no outstanding references to it:

    Note that other applications on the system can reuse a device’s connection so the above must be true for all applications on the system. Two examples are:

    • When the OS provided settings page is open.
    • When the device supports HID over GATT profile and is paired.

    If the above guidance does not resolve your issue please provide sample code for us to reproduce and diagnose the issue. Please include the name and model of the remote device in question or you can provide an ‘air trace’ of interacting with the device if it’s not available in retail.

    Thanks,

    Frank


    Monday, July 27, 2015 11:19 PM

All replies

  • Hi Dominik,

    For Bluetooth LE devices our APIs do not provide direct control over the connection to the device. Instead our stack will disconnect the device after a one second timeout if there are no outstanding references to it:

    Note that other applications on the system can reuse a device’s connection so the above must be true for all applications on the system. Two examples are:

    • When the OS provided settings page is open.
    • When the device supports HID over GATT profile and is paired.

    If the above guidance does not resolve your issue please provide sample code for us to reproduce and diagnose the issue. Please include the name and model of the remote device in question or you can provide an ‘air trace’ of interacting with the device if it’s not available in retail.

    Thanks,

    Frank


    Monday, July 27, 2015 11:19 PM
  • Thanks for your answer but it didn't resolve my problem.

    Here's my source code:

    using (service = await GattDeviceService.FromIdAsync(id))
    {
      firstCharacteristic = service.GetCharacteristics(new Guid(firstGuid))[0];
    
      GattCommunicationStatus status =
        await firstCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
    
      firstCharacteristic.ValueChanged += ValueChangedMethod;
    
      if (status == GattCommunicationStatus.Unreachable)
      {
        // do something..
      }
    
      secondCharacteristic = service.GetCharacteristics(new Guid(secondGuid))[0];
    
      // do bluetooth communication
      // ...
    
      // cleanup
      if (firstCharacteristic != null)
      {
        firstCharacteristic.ValueChanged -= ValueChangedMethod;
    
        status = await firstCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None);
    
        firstCharacteristic= null;
      }
    
      if (secondCharacteristic != null)
      {
        secondCharacteristic = null;
      }
    }

    I noticed that whatever communication is done, the connection is never getting closed. If i do no communication (like writing or setting descriptor) there is no opened connection.

    So I tried to do it with shortest code possible in another project to make sure nothing else is called:

    public async static Task TestConnection(Guid serviceGuid, Guid characteristicGuid)
    {
      var deviceSelector = GattDeviceService.GetDeviceSelectorFromUuid(serviceGuid);
    
      var deviceInformations = await DeviceInformation.FindAllAsync(deviceSelector, null);
    
      var id = deviceInformations[1].Id;
    
      using (var service = await GattDeviceService.FromIdAsync(id))
      {
        var characteristic = service.GetCharacteristics(characteristicGuid))[0];
    
        try
        {
          var writer = new DataWriter();
          writer.WriteBytes(new byte[] { 0x00, 0x01 });
          await characteristic.WriteValueAsync(writer.DetachBuffer());
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
        }
    
        characteristic = null;
      }
    }

    Problem is as expected still the same. Connection is still opened, even after closing the program. I'm testing with our own hardware but I can tell you HID is not supported and the settings page of the OS is not opened. I am neither using DeviceConnectionChangeTrigger nor GattCharacteristicNotificationTrigger.

    I can't provide you 'air trace' since I have no software to capture it. Is there anything available on Windows (freeware if possible)?

    Best regards,

    Dominik





    Tuesday, July 28, 2015 12:20 PM
  • I am also trying to disconnect a BLE device and I think that it is not possible. Only partial solution I have found is to disable a re-enable the BT USB host device but this cannot be done programatically without been an admin user, so it is not a valid solution to me.
    • Edited by Luis Curiel Tuesday, September 1, 2015 7:22 AM
    Tuesday, September 1, 2015 7:22 AM
  • We are in 2018 and still having problems with this topic what is doing Microsoft, sleeping!?

    I could stop receiving more values but not disconnect the BLE device, I have check the bluetoothLEDevice.ConnectionStatus and still connected i am using all the 


    bluetoothLEDevice?.Dispose();
    bluetoothLEDevice = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();


    and the device continue lighting like is connected.

    how to really disconnect this devices?.
    Windows is not a good OS to work with BLE....

    Tuesday, February 20, 2018 12:49 PM
  • Thank you so much hotlips in github it worked perfectly.
    so conclusion Try doing this will work:

    removing all the characteristics Characteristic_ValueChanged for each service :

    characteristic.ValueChanged -= Characteristic_ValueChanged;

    after you have to use the .Dispose() for each service

    service.Dispose();

    Then remove the ConnectionStatusChanged:

    bluetoothLEDevice.ConnectionStatusChanged -= ConnectionStatusChangeHandlerAsync;
    bluetoothLEDevice?.Dispose();
    bluetoothLEDevice = null;
    GC.Collect();

    Thanks again hotlips in github :)
    Hope this help more people :)

    Friday, March 9, 2018 12:27 PM