newAM / idasen

Python API and CLI for the ikea IDÅSEN desk.
MIT License
119 stars 20 forks source link

Failed to connect when device is already connected #29

Open jlsjonas opened 3 years ago

jlsjonas commented 3 years ago

As per the Prerequisites

The desk should be connected and paired to the computer.

However, I was unable to execute a command until I specifically DISCONNECTED the device.

My theory: When f.e. executing bluetoothctl I'm directly on the desk already, so the script is likely trying to connect the desk with another device; instead of just doing its thing

As being connected already would avoid scanning & connecting; thus significantly reducing execution time (especially for simple commands) I'd propose to actually check if we're already connected, and just communicate directly in that case, instead of trying to scan & connect

OS: Ubuntu 20.04 bleak is using bluez as backend (in my case) edit: regarding timing, a no-op (simple read) takes 4-5 seconds currently

newAM commented 3 years ago

Does the branch issue/29 fix this?

I am on a new PC without integrated bluetooth, cannot test this myself until I get a dongle.

jlsjonas commented 3 years ago

@newAM sadly enough it doesn't, it gets stuck on the listing/scanning step in bluez; it seems that the scan doesn't return already connected devices (which makes sense I guess); disconnecting from the desk (even while it's already scanning, as long as not all tries have been exhausted) makes it connect to it and report back.

Sadly enough my python knowledge is quite limited, so I'm not how to help exactly, but it seems that the issue would be in here https://github.com/newAM/idasen/blob/master/idasen/__init__.py#L234

edit: or an issue with BleakClient itself? https://github.com/newAM/idasen/blob/master/idasen/__init__.py#L84

edit2: looks like it's a known limitation of Bleak https://github.com/hbldh/bleak/issues/367#issuecomment-740132661 the fix is that you're supposed to ask Bluez over DBus yourself

newAM commented 3 years ago

The last person said:

I thought Bleak would ask BlueZ over DBus whether the connection exists already but I am okay to do it myself.

Would you know how to do that?

My bluez/dbus experience is limited, I chose bleak simply because it looked like the only bluetooth back-end receiving regular bugfixes.

Edit: Also, sorry for the slow replies, work has been keeping me rather busy on weekdays.

jlsjonas commented 3 years ago

Sadly enough not really 😅 As I mentioned my python knowledge in general is quite limited; I was considering extending one of the node.js-based versions (with IFTTT/google integration) but it seemed like they all had significant problems during quick tests at least.

No problem, work is keeping me quite busy too

jlsjonas commented 3 years ago

https://stackoverflow.com/a/16129705/620141 might be useful for a native connection? (The tutorial in particular)

newAM commented 3 years ago

Huh, it appears the python socket interface will also just work. All that looks pretty spartan though, and I don't know as much about host-side BLE as I would like :sweat_smile:, will probably be a while before I get to it.

jlsjonas commented 3 years ago

I've just completed a PoC in Node connecting the desk through IFTTT with google assistant, and I must say it's working AMAZINGLY so far :sunglasses: I'll be posting the code soon after some polishing work, in case you're interested