Skip to content

Commit 0c0c16f

Browse files
committed
BleakClient: Add pairing_callbacks parameter to constructor
1 parent 7a732b7 commit 0c0c16f

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

bleak/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ class BleakClient:
349349
Callback that will be scheduled in the event loop when the client is
350350
disconnected. The callable must take one argument, which will be
351351
this client object.
352+
pairing_callbacks:
353+
Optional callbacks used in the pairing process (e.g. displaying,
354+
confirming, requesting pin). If provided here instead of to the
355+
:meth:`pair` method as ``callbacks`` parameter, device will be
356+
implicitly paired during connection establishment. This is useful
357+
for devices sending Slave Security Request immediately after
358+
connection, requiring pairing before GATT service discovery.
352359
timeout:
353360
Timeout in seconds passed to the implicit ``discover`` call when
354361
``address_or_ble_device`` is not a :class:`BLEDevice`. Defaults to 10.0.
@@ -385,6 +392,7 @@ def __init__(
385392
self,
386393
address_or_ble_device: Union[BLEDevice, str],
387394
disconnected_callback: Optional[Callable[[BleakClient], None]] = None,
395+
pairing_callbacks: Optional[BaseBleakAgentCallbacks] = None,
388396
*,
389397
timeout: float = 10.0,
390398
winrt: WinRTClientArgs = {},
@@ -398,6 +406,7 @@ def __init__(
398406
self._backend = PlatformBleakClient(
399407
address_or_ble_device,
400408
disconnected_callback=disconnected_callback,
409+
pairing_callbacks=pairing_callbacks,
401410
timeout=timeout,
402411
winrt=winrt,
403412
**kwargs,
@@ -507,9 +516,10 @@ async def pair(
507516
508517
Args:
509518
callbacks:
510-
Optional callbacks for confirming or requesting pin. This is
511-
only supported on Linux and Windows. If omitted, the OS will
512-
handle the pairing request.
519+
Optional callbacks used in the pairing process (e.g. displaying,
520+
confirming, requesting pin).
521+
This is only supported on Linux and Windows.
522+
If omitted, the OS will handle the pairing request.
513523
514524
Returns:
515525
Always returns ``True`` for backwards compatibility.

bleak/backends/bluezdbus/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):
9696
# used to override mtu_size property
9797
self._mtu_size: Optional[int] = None
9898

99+
if kwargs.get("pairing_callbacks"):
100+
warnings.warn(
101+
"Pairing on connect not yet implemented for BlueZ",
102+
RuntimeWarning,
103+
stacklevel=2,
104+
)
105+
99106
def close(self):
100107
self._bus.disconnect()
101108

bleak/backends/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class BaseBleakClient(abc.ABC):
3535
disconnected_callback (callable): Callback that will be scheduled in the
3636
event loop when the client is disconnected. The callable must take one
3737
argument, which will be this client object.
38+
pairing_callbacks (BaseBleakAgentCallbacks):
39+
Optional callbacks otherwise provided as ``callbacks`` parameter to the
40+
:meth:`pair` method. If provided here, device will be implicitly paired
41+
during connection establishment.
3842
"""
3943

4044
def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):

bleak/backends/corebluetooth/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import asyncio
77
import logging
88
import uuid
9+
import warnings
910
from typing import Optional, Union
1011

1112
from CoreBluetooth import (
@@ -52,6 +53,13 @@ def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):
5253
self._delegate: Optional[PeripheralDelegate] = None
5354
self._central_manager_delegate: Optional[CentralManagerDelegate] = None
5455

56+
if kwargs.get("pairing_callbacks"):
57+
warnings.warn(
58+
"Pairing is not available in Core Bluetooth.",
59+
RuntimeWarning,
60+
stacklevel=2,
61+
)
62+
5563
if isinstance(address_or_ble_device, BLEDevice):
5664
(
5765
self._peripheral,

bleak/backends/p4android/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):
4545
self.__gatt = None
4646
self.__mtu = 23
4747

48+
if kwargs.get("pairing_callbacks"):
49+
warnings.warn(
50+
"pairing_callbacks are ignored on Android", RuntimeWarning, stacklevel=2
51+
)
52+
4853
def __del__(self):
4954
if self.__gatt is not None:
5055
self.__gatt.close()

bleak/backends/winrt/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ def __init__(
204204
self._session_status_changed_token: Optional[EventRegistrationToken] = None
205205
self._max_pdu_size_changed_token: Optional[EventRegistrationToken] = None
206206

207+
if kwargs.get("pairing_callbacks"):
208+
warnings.warn(
209+
"pairing_callbacks not yet implemented for Windows",
210+
RuntimeWarning,
211+
stacklevel=2,
212+
)
213+
207214
def __str__(self):
208215
return f"{type(self).__name__} ({self.address})"
209216

0 commit comments

Comments
 (0)