Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BleakClient ignores adaptar argument if given BLEDevice from discover without adapter argument #1328

Closed
r-bt opened this issue Jun 8, 2023 · 2 comments

Comments

@r-bt
Copy link

r-bt commented Jun 8, 2023

  • bleak version: 0.20.2
  • Python version: 3.10.6
  • Operating System: Raspberry Pi OS (bullseye)
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.66

Description

I've connected an external Bluetooth adapter to a Raspberry Pi 4 (which has internal Bluetooth). I'm trying to connect to 15 BLE devices by first scanning for all the devices (using one adapter) and then connecting to those devices splitting the connections across two adapters

What I Did

Running hciconfig I can see that there are two adapters

hci1:	Type: Primary  Bus: USB
	BD Address: 5C:F3:70:A9:E7:FC  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:15910934 acl:330217 sco:0 events:267372 errors:0
	TX bytes:9732 acl:282 sco:0 commands:619 errors:0

hci0:	Type: Primary  Bus: UART
	BD Address: DC:A6:32:E5:4F:F0  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:5350602 acl:209517 sco:0 events:35560 errors:0
	TX bytes:16433 acl:472 sco:0 commands:290 errors:0

Then I run

devices = await BleakScanner.discover()

ret = []

for device in devices:
    if (device.name.startswith("SB")):
        ret.append(device)

clients = []

for bot in ret[:1]:
    client = BleakClient(bot, adapter="hci0")
    await client.connect()
    print("Connected!")
    clients.append(client)

wait = input("Disconnect?")

print("Disconnecting")

for client in clients:
    await client.disconnect()

print("Disconnected!")

and after disconnecting running hciconfig again gives:

hci1:	Type: Primary  Bus: USB
	BD Address: 5C:F3:70:A9:E7:FC  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:16012231 acl:330239 sco:0 events:270334 errors:0
	TX bytes:10438 acl:304 sco:0 commands:684 errors:0

hci0:	Type: Primary  Bus: UART
	BD Address: DC:A6:32:E5:4F:F0  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:5433649 acl:209517 sco:0 events:37734 errors:0
	TX bytes:16433 acl:472 sco:0 commands:290 errors:0

where the TX bytes: for hci0 doesn't change. I also had bluetoothctl running at the same time and it logs

Primary Service (Handle 0x0000)
	/org/bluez/hci1/dev_C6_E7_40_7B_1B_35/service0008
	00001801-0000-1000-8000-00805f9b34fb
	Generic Attribute Profile

indicating the device was connected to using hci1.

Similar issues

This appears similar to #360

@r-bt r-bt changed the title Ignoring adapter argument in BleakClient BleakClient ignores adaptar argument if given BLEDevice from discover without adapter argument Jun 8, 2023
@r-bt r-bt changed the title BleakClient ignores adaptar argument if given BLEDevice from discover without adapter argument BleakClient ignores adaptar argument if given BLE Device from discover without adapter argument Jun 8, 2023
@r-bt r-bt changed the title BleakClient ignores adaptar argument if given BLE Device from discover without adapter argument BleakClient ignores adaptar argument if given BLEDevice from discover without adapter argument Jun 8, 2023
@dlech
Copy link
Collaborator

dlech commented Jun 8, 2023

I think you need to use the adapter arg on BleakScanner rather than BleakClient. The devices returned by the scanner know which adapter was used for scanning and will use the same adapter for connecting.

@r-bt
Copy link
Author

r-bt commented Jun 9, 2023

Yep using adapter on BleakScanner rather than BleakClient fixed the issue. Thanks

@r-bt r-bt closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants