OpenBluetoothToolbox / SimpleBLE

SimpleBLE - the all-in-one Bluetooth library for MacOS, iOS, Windows, Linux and Android.
https://www.simpleble.org
Other
715 stars 120 forks source link

Can I force disconnection of an "old" connection in Python? #367

Open ullix opened 1 day ago

ullix commented 1 day ago

My code works, but every now and then there is a crash of my code e.g. due to an unexpected response of the Bluetooth device, not foreseen in my code.

This usually leaves the BT device connected, and in the next BT-scan I can no longer find the device. And then obiously also not connect again. While I know how to manually "remove" the BT device - annoying but working - a user of my code may not have that knowledge.

I wonder if it is possible to force disconnection of that "old" connection via simplepyble? Like in the same way as my Linux debian12 does it?

eriklins commented 1 day ago

This might be due to the settings of specific BLE connections parameters like "Supervision Timeout". Your device won't consider the link as broken and hence internally drop the connection (and restart advertising) until the Supervision Timeout expired. This timeout can be up to 32 seconds according to the BT spec. Not sure what your device uses, it's part of the negotiation of connection parameters after a connection was engaged. To my knowledge there is no possibility to set this with SimpleBLE or from an OS perspective. Given this, try waiting min. 32 seconds and see if your device pops up in the scanning again. If so, it the sup. timeout and there is likely nothing to do about it.

ullix commented 1 day ago

Well, my BT device (DMM OW18E) does not seem to adhere to any 32 sec limit :-(.

I tested by removing the final peripheral.disconnect() in my code, exited the code, and waited. Even after 4 min the connection was still active, and a new BT scan failed to find the device.

I am on Linux Mint LMDE6 (debian12), which uses the blueman-manager for bluetooth handling. In it I can click on the device and select remove and the device is gone. A subsequent BT scan does find the device. Is it not possible to do this via simplepyble?

Any other way to make it possible?

eriklins commented 1 day ago

I guess if you just terminate your program without disconnecting, the PC's BT controller just keeps the connection active since no one told him to drop it. Might hence be a useful enhancement of SimpleBLE to implement something like simpleble_adapter_get_connected_peripherals_count() and simpleble_adapter_get_connected_peripjerals_handle() similar to the ...scan_get... functions. Not sure if that would be possible from what the OS makes available, maybe @kdewald can comment on it and consider this as an enhancement.

ullix commented 1 day ago

Sure would be a welcom feature!