Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disconnect() doesn't disconnect from device #133

Closed
ghost opened this issue Dec 5, 2019 · 8 comments
Closed

disconnect() doesn't disconnect from device #133

ghost opened this issue Dec 5, 2019 · 8 comments
Assignees
Labels
Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend
Milestone

Comments

@ghost
Copy link

ghost commented Dec 5, 2019

  • bleak version: 0.5.1
  • Python version: 3.6
  • Operating System: Windows 10

Description

I am currently writing an application to connect to a BLE device, write to some characteristics and then disconnect from the device. From our tests, bleak's disconnect API doesn't always send the disconnect command to the device (seen using a snifer). There are cases that the devices remain connected after calling disconnect but bleak returns that they are already disconnected. Not sure where the problem might be, is there any patch for this problem?

What I Did

I tried to disconnect, the return is True but looking at the BLE snifer the devices still transmit empty PDUs to maintain the connection. Calling self.client.is_connected() returns False. The connection has not been terminated.

        try:
             response = await self.client.disconnect()  #self.client is the BleakClient 
             print("DISCONNECT RESPONSE", response)
        except BleakError:
              log.error("Device couldn't be disconnected")
        return response  #Returns true
@hbldh
Copy link
Owner

hbldh commented Jan 5, 2020

Bleak uses, via pythonnet, the C# UWP libraries for connecting to Bluetooth devices.
I have followed the Universal Windows Platform (UWP) app samples method of disconnecting from a BluetoothLEDevice. That being said, it might be that Bleak, pythonnet and/or the C# bridge I have developed delay the disconnecting.

The Dispose method will eventually close the connection. There is a Close method that can be called, but I decided against it to avoid potential errors.

What is the real problem of the device not being freed immediately?

@hbldh hbldh self-assigned this Jan 5, 2020
@hbldh hbldh added the Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend label Jan 5, 2020
@mitchmccarthy
Copy link

mitchmccarthy commented Jan 9, 2020

The peripheral device is intended to drop to a low-power mode after the disconnection, but this issue is causing the connection to persist even though the bleak client says it has disconnected.

We set up a BLE sniffer while we reproduced the issue, and there doesn't seem to be any TERMINATE signals sent over the air. What kind of timeframe would you expect the connection to be closed after the Dispose() method is called? We're seeing the connection remain for several minutes after, our peripheral has its own internal timer to force a disconnect as a redundancy for now.

@hbldh
Copy link
Owner

hbldh commented Jan 9, 2020

@mitchmccarthy This SO issue descibes the problems Bleak faces as well: https://stackoverflow.com/questions/39599252/windows-ble-uwp-disconnect

I would like to be able to close it, but something seems to be left in the BluetoothLEDevice object which makes garbage collection not collecting it. AS to the timeframe, I have no idea. Not more than a minute?

If the process in which Bleak was run is killed, is the connection severed then? Or is it still present?

If you insert specific handling in your device due to Bleak's problems, then maybe this project isn't for you. There are other BLE projects which are production ready; I do not guarantee any functionality in Bleak; it is a spare time exploration project of mine.

@mitchmccarthy
Copy link

If the process (python shell) is killed then yeah the disconnect occurs pretty much immediately, so I think you're right about some hanging reference.

We've already looked at other options, your project was very simple to drop into our application and aside from this issue it worked great so we were hoping to continue with it. I totally understand that this is a work-in-progress project, and thanks for taking the time to investigate the issue.

@hbldh hbldh added this to the Version 0.7.0 milestone Jun 2, 2020
@hbldh hbldh modified the milestones: Version 0.7.0, Version 0.7.X Jun 30, 2020
@hbldh hbldh modified the milestones: Version 0.7.X, Version 0.8.0 Jul 9, 2020
hbldh added a commit that referenced this issue Jul 20, 2020
Version 0.5.0 of BleakUWPBridge, with some modified methods and implementing ``IDisposable``.
Cleanups, disposing of objects and creating new``BleakBridge`` instances each disconnect.
hbldh added a commit that referenced this issue Jul 20, 2020
@hbldh hbldh mentioned this issue Sep 2, 2020
@hbldh
Copy link
Owner

hbldh commented Sep 25, 2020

This is probably solved in version 0.8.0. Will close for now.

@hbldh hbldh closed this as completed Sep 25, 2020
@dlech
Copy link
Collaborator

dlech commented Apr 8, 2021

Related info:

https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/9eae39ff-f6ca-4aa9-adaf-97450f2b4a6c/disconnect-bluetooth-low-energy

I'm currently having trouble getting a device to disconnect on Windows 10 with Bleak 0.11.0.

@dlech
Copy link
Collaborator

dlech commented Apr 8, 2021

And rebooting the computer seems to have fixed the problem.

@megholm
Copy link

megholm commented Nov 30, 2022

If the process (python shell) is killed then yeah the disconnect occurs pretty much immediately, so I think you're right about some hanging reference.

I'll second that.
(Windows 10, Bleak 0.19.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend
Projects
None yet
Development

No branches or pull requests

4 participants