Scan for device by name, connect, and subscribe to TX notifications.
(self)
| 52 | self._rx_queue.put_nowait(text) |
| 53 | |
| 54 | async def connect(self) -> None: |
| 55 | """Scan for device by name, connect, and subscribe to TX notifications.""" |
| 56 | print(f" [BLE] Scanning for '{self._device_name}'...") |
| 57 | device = await BleakScanner.find_device_by_name( |
| 58 | self._device_name, timeout=self._scan_timeout |
| 59 | ) |
| 60 | if device is None: |
| 61 | raise RuntimeError( |
| 62 | f"BLE device '{self._device_name}' not found within {self._scan_timeout}s" |
| 63 | ) |
| 64 | print(f" [BLE] Found device: {device.name} ({device.address})") |
| 65 | |
| 66 | client = BleakClient(device, timeout=30.0) |
| 67 | await client.connect() |
| 68 | self._client = client |
| 69 | print(f" [BLE] Connected to {device.address}") |
| 70 | |
| 71 | # Subscribe to NOTIFY on TX characteristic |
| 72 | await client.start_notify(BLE_CHAR_TX_UUID, self._notification_handler) |
| 73 | print(f" [BLE] Subscribed to TX notifications") |
| 74 | |
| 75 | async def close(self) -> None: |
| 76 | """Disconnect from BLE device.""" |