Skip to content

Commit

Permalink
fix pandora connection waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
barbibulle committed Feb 18, 2025
1 parent 73d2b54 commit dfb92e8
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions bumble/pandora/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,7 @@ async def extended_advertise(
scan_response_data=scan_response_data,
)

pending_connection: asyncio.Future[bumble.device.Connection] = (
asyncio.get_running_loop().create_future()
)
connections: asyncio.Queue[bumble.device.Connection] = asyncio.Queue()

if request.connectable:

Expand All @@ -382,7 +380,7 @@ def on_connection(connection: bumble.device.Connection) -> None:
connection.transport == BT_LE_TRANSPORT
and connection.role == BT_PERIPHERAL_ROLE
):
pending_connection.set_result(connection)
connections.put_nowait(connection)

self.device.on('connection', on_connection)

Expand All @@ -397,8 +395,7 @@ def on_connection(connection: bumble.device.Connection) -> None:
await asyncio.sleep(1)
continue

connection = await pending_connection
pending_connection = asyncio.get_running_loop().create_future()
connection = await connections.get()

cookie = any_pb2.Any(value=connection.handle.to_bytes(4, 'big'))
yield AdvertiseResponse(connection=Connection(cookie=cookie))
Expand Down Expand Up @@ -492,14 +489,16 @@ async def legacy_advertise(
target = Address(target_bytes, Address.RANDOM_DEVICE_ADDRESS)
advertising_type = AdvertisingType.DIRECTED_CONNECTABLE_LOW_DUTY

connections: asyncio.Queue[bumble.device.Connection] = asyncio.Queue()

if request.connectable:

def on_connection(connection: bumble.device.Connection) -> None:
if (
connection.transport == BT_LE_TRANSPORT
and connection.role == BT_PERIPHERAL_ROLE
):
pending_connection.set_result(connection)
connections.put_nowait(connection)

self.device.on('connection', on_connection)

Expand All @@ -517,12 +516,8 @@ def on_connection(connection: bumble.device.Connection) -> None:
await asyncio.sleep(1)
continue

pending_connection: asyncio.Future[bumble.device.Connection] = (
asyncio.get_running_loop().create_future()
)

self.log.debug('Wait for LE connection...')
connection = await pending_connection
connection = await connections.get()

self.log.debug(
f"Advertise: Connected to {connection.peer_address} (handle={connection.handle})"
Expand Down

0 comments on commit dfb92e8

Please sign in to comment.