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

Implement device button click to conncet to device

parent 5cbe207a
No related branches found
No related tags found
No related merge requests found
......@@ -16,14 +16,16 @@ class DeviceListWidget(Widget):
self._devices_buttons_map = {}
@mainthread
def update_device_buttons(self, devices):
def update_device_buttons(self, buttons_map):
Logger.debug("Updating device buttons")
self._devices_buttons_map = devices
self.device_buttons.clear_widgets()
for label, device in self._devices_buttons_map.items():
Logger.debug("Adding button for %s" % label)
btn = Button(text=label)
for address, info in buttons_map.items():
label, cb_connect = info
Logger.debug("Adding button for %s (%s)" % (address, label))
btn = Button(text=label, on_press=cb_connect)
self.device_buttons.add_widget(btn)
......@@ -34,7 +36,20 @@ class MynitCompanionApp(App):
self._bluetooth = MynitBluetooth(self)
def update_device_list(self, devices):
self.widget.update_device_buttons({device.getAddress(): device for device in devices})
Logger.debug("Updating device buttons")
buttons_map = {}
for device in devices:
if not device.getName():
Logger.info("Device %s skipped due to missing name" % device.getAddress())
continue
def cb_connect(*args, **kwargs):
self._bluetooth.connect_by_device_address(device.getAddress())
buttons_map[device.getAddress()] = (device.getName(), cb_connect)
self.widget.update_device_buttons(buttons_map)
def build(self):
self.widget = DeviceListWidget()
......
......@@ -32,6 +32,11 @@ class MynitBluetooth(BluetoothDispatcher):
self._app.update_device_list(self._available_devices)
def connect_by_device_address(self, address):
Logger.info("Connecting to %s" % address)
self.stop_scan()
return super().connect_by_device_address(address)
def on_connection_state_change(self, status, state):
Logger.debug("Connection state changed")
......@@ -44,6 +49,7 @@ class MynitBluetooth(BluetoothDispatcher):
self.close_gatt()
self._current_device = None
self._characteristics = None, None
self.start_scan()
def on_services(self, status, services):
self._characteristics = (services.search(_UART_SERVICE_TX_UUID), services.search(_UART_SERVICE_RX_UUID))
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