diff --git a/bleak/__init__.py b/bleak/__init__.py index f37f1058..26065c2a 100644 --- a/bleak/__init__.py +++ b/bleak/__init__.py @@ -357,6 +357,8 @@ class BleakClient: backend: Used to override the automatically selected backend (i.e. for a custom backend). + skip_service_discovery: + Do not perform automatic GATT service discovery in :meth:`connect`. **kwargs: Additional keyword arguments for backwards compatibility. @@ -389,6 +391,7 @@ def __init__( timeout: float = 10.0, winrt: WinRTClientArgs = {}, backend: Optional[Type[BaseBleakClient]] = None, + skip_service_discovery: bool = False, **kwargs, ): PlatformBleakClient = ( @@ -400,6 +403,7 @@ def __init__( disconnected_callback=disconnected_callback, timeout=timeout, winrt=winrt, + skip_service_discovery=skip_service_discovery, **kwargs, ) diff --git a/bleak/backends/bluezdbus/client.py b/bleak/backends/bluezdbus/client.py index c0f57e38..ac622aab 100644 --- a/bleak/backends/bluezdbus/client.py +++ b/bleak/backends/bluezdbus/client.py @@ -219,13 +219,11 @@ def on_value_changed(char_path: str, value: bytes) -> None: ) ) - # - # We will try to use the cache if it exists and `dangerous_use_bleak_cache` - # is True. - # - await self.get_services( - dangerous_use_bleak_cache=dangerous_use_bleak_cache - ) + if not self._skip_service_discovery: + # We will try to use the cache if it exists and `dangerous_use_bleak_cache` is True. + await self.get_services( + dangerous_use_bleak_cache=dangerous_use_bleak_cache + ) return True except BaseException: diff --git a/bleak/backends/client.py b/bleak/backends/client.py index c303c2f2..32ef034b 100644 --- a/bleak/backends/client.py +++ b/bleak/backends/client.py @@ -35,14 +35,21 @@ class BaseBleakClient(abc.ABC): disconnected_callback (callable): Callback that will be scheduled in the event loop when the client is disconnected. The callable must take one argument, which will be this client object. + skip_service_discovery (bool): Do not perform automatic GATT service discovery in :meth:`connect`. """ - def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs): + def __init__( + self, + address_or_ble_device: Union[BLEDevice, str], + skip_service_discovery: bool = False, + **kwargs, + ): if isinstance(address_or_ble_device, BLEDevice): self.address = address_or_ble_device.address else: self.address = address_or_ble_device + self._skip_service_discovery = skip_service_discovery self._services: Optional[BleakGATTServiceCollection] = None self._timeout = kwargs.get("timeout", 10.0) diff --git a/bleak/backends/corebluetooth/client.py b/bleak/backends/corebluetooth/client.py index d1481cc9..74981d58 100644 --- a/bleak/backends/corebluetooth/client.py +++ b/bleak/backends/corebluetooth/client.py @@ -113,7 +113,8 @@ def disconnect_callback(): await manager.connect(self._peripheral, disconnect_callback, timeout=timeout) # Now get services - await self.get_services() + if not self._skip_service_discovery: + await self.get_services() return True diff --git a/bleak/backends/p4android/client.py b/bleak/backends/p4android/client.py index 201ac17c..498835c1 100644 --- a/bleak/backends/p4android/client.py +++ b/bleak/backends/p4android/client.py @@ -100,14 +100,9 @@ async def connect(self, **kwargs) -> bool: resultApi="onMtuChanged", ) - logger.debug("discovering services...") - await self.__callbacks.perform_and_wait( - dispatchApi=self.__gatt.discoverServices, - dispatchParams=(), - resultApi="onServicesDiscovered", - ) + if not self._skip_service_discovery: + await self.get_services() - await self.get_services() except BaseException: # if connecting is canceled or one of the above fails, we need to # disconnect @@ -248,6 +243,13 @@ async def get_services(self) -> BleakGATTServiceCollection: if self._services: return self._services + logger.debug("Discovering services...") + await self.__callbacks.perform_and_wait( + dispatchApi=self.__gatt.discoverServices, + dispatchParams=(), + resultApi="onServicesDiscovered", + ) + services = BleakGATTServiceCollection() logger.debug("Get Services...")