spherov2.py icon indicating copy to clipboard operation
spherov2.py copied to clipboard

Interest in specifying bleak adapter

Open r-bt opened this issue 1 year ago • 4 comments

Hey, I was just wanted to check if you'd be interested in a PR to add the option to specify the specific Bluetooth adapter bleak should use?

r-bt avatar Jun 06 '23 17:06 r-bt

a PR will be more than welcomed. Sorry that I am quite busy recently so cannot reply to issues promptly.

superfashi avatar Jun 06 '23 18:06 superfashi

Awesome! Also when I was looking at bleak_adapter.py I noticed that you're passing the bluetooth device address as opposed to the BLEDevice to Bleak Client (e.g. bleak.BleakClient(address)). Is there a reason for this? In the bleak documentation there's a warning against doing so as it (from https://bleak.readthedocs.io/en/latest/api/client.html)

causes the connect() method to implicitly call BleakScanner.discover(). This is known to cause problems when trying to connect to multiple devices at the same time.

and

macOS does not provide access to the Bluetooth address for privacy/ security reasons. Instead it creates a UUID for each Bluetooth device which is used in place of the address on this platform.

which could actually be the cause of https://github.com/artificial-intelligence-class/spherov2.py/issues/35

r-bt avatar Jun 06 '23 21:06 r-bt

Also I'm just wondering about why a new event loop and thread are created for each BleakClient? From digging around this works fine when passing Bluetooth addresses but on macOS if I pass a BLEDevice it gives this error

got Future <Future pending> attached to a different loop

Apparently this is a known issue with Bleak (see https://github.com/hbldh/bleak/discussions/1191 and https://github.com/hbldh/bleak/issues/242) so they recommend against having multiple threads and event loops

the design paradigm for bleak is to have multiple connections all under 1 event loop in 1 thread

I can make a PR to address these issues but I just wanted to check if that would be something you'd be interested in?

r-bt avatar Jun 07 '23 16:06 r-bt

Apparently BLEDevice uses future which attaches with a single event loop.

There is an async branch where I tried to write everything in async fashion. But the API is way too confusing.

I'm interested in your PR on this.

superfashi avatar Jun 07 '23 22:06 superfashi