kevincar / bless

Cross-platform Bluetooth Low Energy Server Python Library
MIT License
78 stars 26 forks source link

Adapter selection #86

Closed Jakeler closed 1 year ago

Jakeler commented 2 years ago

Describe the solution you'd like If there are multiple BLE adapters on the host it should be possible to specify which one is used for the server. Currently it picks just the first it finds on dbus: https://github.com/kevincar/bless/blob/v0.2.4/bless/backends/bluezdbus/dbus/utils.py#L33

I would keep the API similar to bleak, like BleakClient(adapter="hci1") or BleakScanner(adapter="hci1") it could be BlessServer(adapter="hci1").

Describe alternatives you've considered Instead of just receiving a string, bless could offer a list_adapters() function. That would allow to get and pass objects with more data, for example the full bluez path, but I am not sure if this is really needed or helpful. In any case such a function could be useful for users to find out adapter names.

Additional context Seems to only be relevant for Linux (including Android on bleak). More info about how this is handled on Mac or Windows is appreciated.

kevincar commented 1 year ago

Apologies for the delay. Linux implementation was a quick fix. Implemented a list_adapters() function as suggested.

As mentioned, the macOS and Windows implementations seem to be more involved. macOS previously provided a developer app called Bluetooth Explorer that allowed users to select the primary HCI adapter to use. With updates to BigSur and Monterey, this is no longer seems possible.

For Windows, there are UWP BluetoothAdaptor classes that represent BLE adapters but currently there is not a clear way to dictate which adapter supplies the GattServiceProvider for building the Gatt tree.

One approach I've started diving into is building compiled binaries that utilize the GattServiceProvider for windows and the CBPeripheralManager on macOS and running debuggers to pinpoint how the underlying API access and select the internal BLE adapters, but this is proving to take a bit more time than I have available for this issue at the moment.

Will merge updates to develop soon.