HAP: wait for pairing event (#551)

This commit is contained in:
William Escande
2024-10-10 11:34:44 -07:00
committed by GitHub
parent f5443a9826
commit 23f46b36b3
2 changed files with 32 additions and 18 deletions

View File

@@ -25,7 +25,7 @@ from bumble.utils import AsyncRunner, OpenIntEnum
from bumble.hci import Address from bumble.hci import Address
from dataclasses import dataclass, field from dataclasses import dataclass, field
import logging import logging
from typing import Dict, List, Optional, Set, Union from typing import Any, Dict, List, Optional, Set, Union
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@@ -271,24 +271,12 @@ class HearingAccessService(gatt.TemplateService):
def on_disconnection(_reason) -> None: def on_disconnection(_reason) -> None:
self.currently_connected_clients.remove(connection) self.currently_connected_clients.remove(connection)
# TODO Should we filter on device bonded && device is HAP ? @connection.on('pairing') # type: ignore
self.currently_connected_clients.add(connection) def on_pairing(*_: Any) -> None:
if ( self.on_incoming_paired_connection(connection)
connection.peer_address
not in self.preset_changed_operations_history_per_device
):
self.preset_changed_operations_history_per_device[
connection.peer_address
] = []
return
async def on_connection_async() -> None: if connection.peer_resolvable_address:
# Send all the PresetChangedOperation that occur when not connected self.on_incoming_paired_connection(connection)
await self._preset_changed_operation(connection)
# Update the active preset index if needed
await self.notify_active_preset_for_connection(connection)
connection.abort_on('disconnection', on_connection_async())
self.hearing_aid_features_characteristic = gatt.Characteristic( self.hearing_aid_features_characteristic = gatt.Characteristic(
uuid=gatt.GATT_HEARING_AID_FEATURES_CHARACTERISTIC, uuid=gatt.GATT_HEARING_AID_FEATURES_CHARACTERISTIC,
@@ -325,6 +313,27 @@ class HearingAccessService(gatt.TemplateService):
] ]
) )
def on_incoming_paired_connection(self, connection: Connection):
'''Setup initial operations to handle a remote bonded HAP device'''
# TODO Should we filter on HAP device only ?
self.currently_connected_clients.add(connection)
if (
connection.peer_address
not in self.preset_changed_operations_history_per_device
):
self.preset_changed_operations_history_per_device[
connection.peer_address
] = []
return
async def on_connection_async() -> None:
# Send all the PresetChangedOperation that occur when not connected
await self._preset_changed_operation(connection)
# Update the active preset index if needed
await self.notify_active_preset_for_connection(connection)
connection.abort_on('disconnection', on_connection_async())
def _on_read_active_preset_index( def _on_read_active_preset_index(
self, __connection__: Optional[Connection] self, __connection__: Optional[Connection]
) -> bytes: ) -> bytes:

View File

@@ -25,6 +25,7 @@ import sys
from bumble import att, device from bumble import att, device
from bumble.profiles import hap from bumble.profiles import hap
from .test_utils import TwoDevices from .test_utils import TwoDevices
from bumble.keys import PairingKeys
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Logging # Logging
@@ -86,6 +87,10 @@ async def hap_client():
devices.connections[0].encryption = 1 # type: ignore devices.connections[0].encryption = 1 # type: ignore
devices.connections[1].encryption = 1 # type: ignore devices.connections[1].encryption = 1 # type: ignore
devices[0].on_pairing(
devices.connections[0], devices.connections[0].peer_address, PairingKeys(), True
)
peer = device.Peer(devices.connections[1]) # type: ignore peer = device.Peer(devices.connections[1]) # type: ignore
hap_client = await peer.discover_service_and_create_proxy( hap_client = await peer.discover_service_and_create_proxy(
hap.HearingAccessServiceProxy hap.HearingAccessServiceProxy