Skip to content

Commit cfd09ba

Browse files
committed
BleakClient: Support performing GATT service disocvery after pairing
1 parent 7f8c72e commit cfd09ba

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

bleak/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,12 @@ async def pair(
524524
BleakPairingFailedError:
525525
if pairing failed (rejected, wrong pin, etc.)
526526
"""
527-
return await self._backend.pair(callbacks, **kwargs)
527+
if await self._backend.pair(callbacks, **kwargs):
528+
# If services were already discovered during connection procedure, this will return cached
529+
# services anyway. If `skip_service_discovery=True` was used, this will perform the discovery.
530+
await self._backend.get_services()
531+
return True
532+
return False
528533

529534
async def unpair(self) -> bool:
530535
"""

examples/pairing_agent.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,19 @@ async def main(addr: str, unpair: bool) -> None:
7878

7979
print("pairing...")
8080

81-
async with BleakClient(device) as client, AgentCallbacks() as callbacks:
81+
async with (
82+
BleakClient(device) as client,
83+
# If the peripheral device sends Slave Security Request immediately upon establishing connection,
84+
# the central device shall respond with the pairing request - usually before starting/finishing
85+
# service discovery, that is before connect() method normally returns. In such cases the pairing
86+
# process usually fails with "Pairing not supported" error - as a workaround,
87+
# BleakClient(device, skip_service_discovery=True) as client,
88+
# can be used, which will execute service discovery only after pairing.
89+
AgentCallbacks() as callbacks,
90+
):
8291
try:
8392
await client.pair(callbacks)
84-
print("pairing successful")
93+
print(f"pairing successful, {len(client.services.services)} services")
8594
except BleakPairingCancelledError:
8695
print("paring was canceled")
8796
except BleakPairingFailedError:

0 commit comments

Comments
 (0)