from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService ble = BLERadio() while True: while ble.connected and any( UARTService in connection for connection in ble.connections ): for connection in ble.connections: if UARTService not in connection: continue uart = connection[UARTService] while ble.connected: one_byte = uart.readline() if one_byte: print(one_byte.decode().strip()) print("disconnected, scanning") for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=1): if UARTService not in advertisement.services: continue ble.connect(advertisement) print("connected") break ble.stop_scan()