30
30
"""
31
31
from bleio import Central , CharacteristicBuffer
32
32
from .uart import NUS_SERVICE_UUID , NUS_RX_CHAR_UUID , NUS_TX_CHAR_UUID
33
+ from .scanner import ScanEntry
33
34
34
35
class UARTClient :
35
36
"""
@@ -46,13 +47,12 @@ class UARTClient:
46
47
from adafruit_ble.uart_client import UARTClient
47
48
from adafruit_ble.scanner import Scanner, ScanEntry
48
49
49
- scanner = Scanner()
50
- uarts = ScanEntry.with_service_uuid(scanner.scan_unique(3), UART.NUS_SERVICE_UUID)
51
- if not uarts:
52
- raise ValueError("No UART for connection")
53
-
54
50
uart_client = UARTClient()
55
- uart_client.connect(uarts[0].address, 5, service_uuids=(UART.NUS_SERVICE_UUID,))
51
+ uart_addresses = uart_client.scan()
52
+ if uart_addresses:
53
+ uart_client.connect(uarts[0].address, 5, service_uuids=(UART.NUS_SERVICE_UUID,))
54
+ else:
55
+ raise Error("No UART servers found.")
56
56
57
57
uart_client.write('abc')
58
58
"""
@@ -91,6 +91,17 @@ def connect(self, address, timeout):
91
91
timeout = self ._timeout ,
92
92
buffer_size = self ._buffer_size )
93
93
94
+ def scan (self , scanner = None , scan_time = 2 ):
95
+ """Scan for Peripherals advertising the Nordic UART Service,
96
+ and return their addresses.
97
+
98
+ :param int scanner: Scanner to use. If not supplied, a local one will
99
+ be created.
100
+ :param float scan_time: scan for this many seconds.
101
+ :return list of Address objects, or an empty list, if none found
102
+ """
103
+ return [se .address for se in
104
+ ScanEntry .with_service_uuid (scanner .scan_unique (scan_time ), NUS_SERVICE_UUID )]
94
105
95
106
def disconnect (self ):
96
107
"""Disconnect from the peripheral."""
0 commit comments