Skip to content
Snippets Groups Projects
Verified Commit 8c501f74 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Try retrying operations in BLE

parent 7fe90078
No related branches found
No related tags found
No related merge requests found
Pipeline #77121 failed
...@@ -5,8 +5,8 @@ from able.filters import ServiceUUIDFilter ...@@ -5,8 +5,8 @@ from able.filters import ServiceUUIDFilter
from kivy.logger import Logger from kivy.logger import Logger
_UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e" _UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
_UART_SERVICE_TX_UUID = "6e400002" _UART_SERVICE_TX_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
_UART_SERVICE_RX_UUID = "6e400003" _UART_SERVICE_RX_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
class MynitBluetooth(BluetoothDispatcher): class MynitBluetooth(BluetoothDispatcher):
...@@ -18,6 +18,7 @@ class MynitBluetooth(BluetoothDispatcher): ...@@ -18,6 +18,7 @@ class MynitBluetooth(BluetoothDispatcher):
self._available_devices = set() self._available_devices = set()
self._device = None self._device = None
self._characteristics = None, None self._characteristics = None, None
self._error_count = 0
Logger.info("Starting BLE scanning") Logger.info("Starting BLE scanning")
# self.start_scan(filters=[ServiceUUIDFilter(_UART_SERVICE_UUID)]) # self.start_scan(filters=[ServiceUUIDFilter(_UART_SERVICE_UUID)])
...@@ -38,7 +39,9 @@ class MynitBluetooth(BluetoothDispatcher): ...@@ -38,7 +39,9 @@ class MynitBluetooth(BluetoothDispatcher):
def on_scan_completed(self): def on_scan_completed(self):
if self._device: if self._device:
# FIXME Add retries self._error_count = 0
self.close_gatt()
self.gatt.refresh()
self.connect_gatt(self._device) self.connect_gatt(self._device)
def on_connection_state_change(self, status, state): def on_connection_state_change(self, status, state):
...@@ -49,15 +52,28 @@ class MynitBluetooth(BluetoothDispatcher): ...@@ -49,15 +52,28 @@ class MynitBluetooth(BluetoothDispatcher):
self.discover_services() self.discover_services()
else: else:
Logger.warning("Connection failed, disconnecting and resetting") Logger.warning("Connection failed, disconnecting and resetting")
self.close_gatt() self._error_count += 1
self._characteristics = None, None if self._error_count <= 2:
self._device = None self.connect_gatt(self._device)
self.start_scan() elif self._error_count <= 3:
self.close_gatt()
self.connect_gatt(self._device)
else:
self._characteristics = None, None
self._device = None
self._error_count = 0
self.start_scan()
def on_services(self, status, services): def on_services(self, status, services):
Logger.info("Services discovered") Logger.info("Services discovered")
self._characteristics = (services.search(_UART_SERVICE_TX_UUID), services.search(_UART_SERVICE_RX_UUID)) self._characteristics = (services.search(_UART_SERVICE_TX_UUID), services.search(_UART_SERVICE_RX_UUID))
self.enable_notifications(self._characteristics[1]) Logger.info(self._characteristics)
if None in self._characteristics:
Logger.warning("UART services not yet found, retrying")
self.discover_services()
else:
Logger.info("UART services found, enabling notifications")
self.enable_notifications(self._characteristics[1])
def on_characteristic_changed(self, characteristic): def on_characteristic_changed(self, characteristic):
Logger.info("Data received") Logger.info("Data received")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment