Skip to content

Commit 6f5fcc5

Browse files
committed
add UART_Client.scan() convenience method
1 parent 672f51c commit 6f5fcc5

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

adafruit_ble/uart_client.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"""
3131
from bleio import Central, CharacteristicBuffer
3232
from .uart import NUS_SERVICE_UUID, NUS_RX_CHAR_UUID, NUS_TX_CHAR_UUID
33+
from .scanner import ScanEntry
3334

3435
class UARTClient:
3536
"""
@@ -46,13 +47,12 @@ class UARTClient:
4647
from adafruit_ble.uart_client import UARTClient
4748
from adafruit_ble.scanner import Scanner, ScanEntry
4849
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-
5450
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.")
5656
5757
uart_client.write('abc')
5858
"""
@@ -91,6 +91,17 @@ def connect(self, address, timeout):
9191
timeout=self._timeout,
9292
buffer_size=self._buffer_size)
9393

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)]
94105

95106
def disconnect(self):
96107
"""Disconnect from the peripheral."""

0 commit comments

Comments
 (0)