toy / blueutil

CLI for bluetooth on OSX: power, discoverable state, list, inquire devices, connect, info, …
https://github.com/toy/blueutil
Other
969 stars 52 forks source link

Disconnect/wait-disconnect issues #70

Closed JoshuaIyalagha closed 1 year ago

JoshuaIyalagha commented 1 year ago

Hello, I am having issues when trying to --disconnect or --wait-disconnect. The command runs but the bluetooth device doesn't disconnect. When I use --wait-disconnect, i get an error that states "Timed out waiting for "(null)" to disconnect" even though I properly inputed the right bluetooth MAC address. Thanks for creating the tool!

toy commented 1 year ago

Few questions: Does this device have a name (checking through preference pane or using bluetil --connected/blueutil --paired/blueutil --recent)? Could you try disconnecting another device same way?

JoshuaIyalagha commented 1 year ago

Hello, thank you for your quick response. Yes the devices do. Yes, I have tried it with multiple devices. The first two are my google nest speakers. The third was my bluetooth headphones.

All connect but don't disconnect via terminal/blueutil. I can disconnect them via the menu bar or by using apps like ToothFairy.

If you need more info, please let me know!

hb1998 commented 1 year ago

I have the exact same issue, it connects but doesn't disconnect. FYI: I have the same issue with bluetoothconnector I'm using a TWS (One plus Buds Z)

paulmiddelkoop commented 1 year ago

@JoshuaIyalagha You say that you try --disconnect or --wait-disconnect, but I found out that you need to combine them.

This works for me:

blueutil --disconnect 04-fe-a1-e4-73-75 --wait-disconnect 04-fe-a1-e4-73-75

I debugged a bit because I tried to fix a bug in the bluetooth extension (Toothpick) for Raycast. I noticed that the underlying problem is that IOBluetoothDevice.closeConnection returns successfully but does not close anything. A simple delay seems to fix the problem. I did not found an elegant solution yet to fix it. But the -wait-disconnect does already seem to fix it on this side.

See also https://github.com/lapfelix/BluetoothConnector/issues/20 where people experience the same problems.

paulmiddelkoop commented 1 year ago

@toy Maybe this gives you some information to fix this problem on the blueutil side?

toy commented 1 year ago

@paulmiddelkoop I'll write thoughts around the problem.

Documentation of closeConnection says This method is synchronous and will not return until the connection has been closed (or the command failed). In the future this API will be changed to allow asynchronous operation., so it doesn't behave according to documentation. I don't know if it is overall a bug somewhere in framework or has something to do with the fact that it is called from a CLI binary (some assumption on part of framework making it not work well in this case). May be clarified by creating a simple GUI app with a button to call closeConnection on a device.

Unless the reason for the problem is discovered, I imagine 2 solutions (besides relying on always combining with --wait-disconnect):

paulmiddelkoop commented 1 year ago

@toy I would go with option 2. I think most users expect that a --disconnect just does what is says. I would not expect that people rely on async disconnect now, otherwise they rely on undocumented behaviour of blueutil.

Also I think it's best that the --connect and --disconnect behave the same way. So either both being async or both being sync. Since the --connect is synchronous, it makes sense to me to do the same thing for --disconnect.

toy commented 1 year ago

Would be great if someone having the problem can check if changes on wait-on-disconnect branch fix them

paulmiddelkoop commented 1 year ago

@toy I tested the branch and it fixes it 🚀 Great job!

toy commented 1 year ago

v2.9.1 is out

@JoshuaIyalagha Could you please check if the issue is fixed for you?

Tocseoj commented 1 year ago

Just adding that I was having the same issue with my magic mouse, but upgrading to v2.9.1 fixed it. Thanks, @toy!