From ce37f2b312e9a2a086257b5f13613192fab85c9a Mon Sep 17 00:00:00 2001 From: Markus Piotrowski Date: Mon, 29 Apr 2024 23:33:18 +0200 Subject: [PATCH] Add files via upload Allow `detection_callback` and `notifying_callback` to be asynchronous. Add `mtu_size` and `address` properties to `Client` --- bleekWare/Client.py | 30 ++++++++++++++++++++++-------- bleekWare/Scanner.py | 8 +++++++- bleekWare/__init__.py | 4 ++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/bleekWare/Client.py b/bleekWare/Client.py index 614fc4c..1bc37bb 100644 --- a/bleekWare/Client.py +++ b/bleekWare/Client.py @@ -4,6 +4,7 @@ import asyncio import functools +import inspect from java import jarray, jbyte, jclass, jint, jvoid, Override, static_proxy from java.util import UUID @@ -103,8 +104,6 @@ def onMtuChanged(self, gatt, mtu, status): """ if status == BluetoothGatt.GATT_SUCCESS: self.client.mtu = mtu - else: - print("Failed to read characteristic") class Client: @@ -126,12 +125,12 @@ def __init__( ).singletonThis if isinstance(address_or_ble_device, BLEDevice): - self.address = address_or_ble_device.address + self._address = address_or_ble_device.address self.device = address_or_ble_device.details else: - self.address = address_or_ble_device + self._address = address_or_ble_device self.device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice( - self.address + self._address ) self.disconnected_callback = ( @@ -146,6 +145,9 @@ def __init__( self.services = [] self.mtu = 23 + def __str__(self): + return f'{self.__class__.__name__}, {self.address}' + async def __aenter__(self): await self.connect() return self @@ -179,9 +181,8 @@ async def connect(self, **kwargs): await asyncio.sleep(0.1) self.services = await self.get_services() + # Ask for max Mtu size self.gatt.requestMtu(517) - await asyncio.sleep(1) - print(f'\n\nmtu: {self.mtu}\n\n') async def disconnect(self): """Disconnect from connected GATT server.""" @@ -239,7 +240,12 @@ async def start_notify(self, char_specifier, callback, *kwargs): while self.notification_callback: if received_data: data = received_data.pop() - callback(char_specifier, bytearray(data)) + if inspect.iscoroutinefunction(callback): + asyncio.create_task( + callback(char_specifier, bytearray(data)) + ) + else: + callback(char_specifier, bytearray(data)) await asyncio.sleep(0.1) async def stop_notify(self, char_specifier): @@ -309,6 +315,14 @@ async def write_gatt_char(self, uuid, data, response=None): def is_connected(self): return False if self.gatt is None else True + @property + def mtu_size(self): + return self.mtu + + @property + def address(self): + return self._address + def _find_characteristic(self, uuid): """Find and return characteristic object by UUID. PRIVATE.""" for service in self.services: diff --git a/bleekWare/Scanner.py b/bleekWare/Scanner.py index 0cec169..90784ad 100644 --- a/bleekWare/Scanner.py +++ b/bleekWare/Scanner.py @@ -3,6 +3,7 @@ """ import asyncio +import inspect import time from java import jclass, jint, jvoid, Override, static_proxy @@ -94,7 +95,12 @@ def onScanResult(self, callbackType, scanResult): scan_result[address] = (new_device, advertisement) if self.scanner.detection_callback: - self.scanner.detection_callback(new_device, advertisement) + if inspect.iscoroutinefunction(self.scanner.detection_callback): + asyncio.create_task( + self.scanner.detection_callback(new_device, advertisement) + ) + else: + self.scanner.detection_callback(new_device, advertisement) class AdvertisementData: diff --git a/bleekWare/__init__.py b/bleekWare/__init__.py index 15b45a8..38689f7 100644 --- a/bleekWare/__init__.py +++ b/bleekWare/__init__.py @@ -41,7 +41,7 @@ class bleekWareError(Exception): class bleekWareCharacteristicNotFoundError(bleekWareError): """A characteristic is not supported by a device.""" - + def __init__(self, uuid): """ uuid (str): UUID of the characteristic which was not found @@ -52,7 +52,7 @@ def __init__(self, uuid): class bleekWareDeviceNotFoundError(bleekWareError): """A device couldn't be found.""" - + def __init__(self, identifier, *args): """ Args: