Skip to content

Commit

Permalink
BleakClient: Add option to skip GATT service discovery in connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanpotocnik committed Nov 22, 2022
1 parent 903fcd6 commit 7f8c72e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
4 changes: 4 additions & 0 deletions bleak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -389,6 +391,7 @@ def __init__(
timeout: float = 10.0,
winrt: WinRTClientArgs = {},
backend: Optional[Type[BaseBleakClient]] = None,
skip_service_discovery: bool = False,
**kwargs,
):
PlatformBleakClient = (
Expand All @@ -400,6 +403,7 @@ def __init__(
disconnected_callback=disconnected_callback,
timeout=timeout,
winrt=winrt,
skip_service_discovery=skip_service_discovery,
**kwargs,
)

Expand Down
12 changes: 5 additions & 7 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion bleak/backends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion bleak/backends/corebluetooth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions bleak/backends/p4android/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...")
Expand Down

0 comments on commit 7f8c72e

Please sign in to comment.