From 8065d12daccce96d1b49894e4fe2d68d73c40c0f Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Tue, 13 Feb 2024 22:10:34 +0100 Subject: [PATCH] python: Various fixes --- gattlib-py/gattlib/adapter.py | 7 +++++-- gattlib-py/gattlib/device.py | 29 ++++++++++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/gattlib-py/gattlib/adapter.py b/gattlib-py/gattlib/adapter.py index f49ee27..8ace24b 100644 --- a/gattlib-py/gattlib/adapter.py +++ b/gattlib-py/gattlib/adapter.py @@ -67,8 +67,11 @@ class Adapter: return ret def close(self): - ret = gattlib.gattlib_adapter_close(self._adapter) + ret = 0 + if self._adapter: + ret = gattlib.gattlib_adapter_close(self._adapter) self._is_opened = False + self._adapter = None return ret # Use a closure to return a method that can be called by the C-library (see: https://stackoverflow.com/a/7261524/6267288) @@ -196,7 +199,7 @@ class Adapter: } ret = gattlib_adapter_scan_eddystone(self._adapter, rssi, eddystone_filters, - gattlib_discovered_device_with_data_type(Adapter.on_discovered_ble_device_with_details), + Adapter.on_discovered_ble_device_with_details, timeout, args) handle_return(ret) diff --git a/gattlib-py/gattlib/device.py b/gattlib-py/gattlib/device.py index 0caead6..a6f23c5 100644 --- a/gattlib-py/gattlib/device.py +++ b/gattlib-py/gattlib/device.py @@ -40,6 +40,7 @@ class Device: self._name = name self._connection = None self.on_connection_callback = None + self.on_connection_error_callback = None # Keep track if notification handler has been initialized self._is_notification_init = False @@ -65,8 +66,12 @@ class Device: def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT): def _on_connection(adapter: c_void_p, mac_address: c_char_p, connection: c_void_p, error: c_int, user_data: py_object): - self._connection = connection - self.on_connection(user_data) + if error: + self._connection = None + self.on_connection_error(error, user_data) + else: + self._connection = connection + self.on_connection(user_data) if self._adapter is None: adapter = None @@ -82,6 +87,11 @@ class Device: if self.on_connection_callback: self.on_connection_callback(self, user_data) + def on_connection_error(self, error: c_int, user_data: py_object): + logging.error("Failed to connect due to error '%s'", error) + if self.on_connection_error_callback: + self.on_connection_error_callback(self, error, user_data) + @property def rssi(self): _rssi = c_int16(0) @@ -92,19 +102,16 @@ class Device: else: return self._adapter.get_rssi_from_mac(self._addr) - @staticmethod - def on_disconnection(user_data): - this = user_data - - this.disconnection_callback(this.disconnection_user_data) - - def register_on_disconnect(self, callback, user_data): + def register_on_disconnect(self, callback, user_data=None): self.disconnection_callback = callback - self.disconnection_user_data = user_data + + def on_disconnection(user_data): + if self.disconnection_callback: + self.disconnection_callback() gattlib_register_on_disconnect(self.connection, gattlib_disconnected_device_python_callback, - gattlib_python_callback_args(Device.on_disconnection, user_data)) + gattlib_python_callback_args(on_disconnection, user_data)) def disconnect(self): if self._connection: