Skip to content

Commit 0f2c39f

Browse files
authored
Merge pull request adafruit#1282 from dhalbert/ble-host-computer
ble_eval examples
2 parents ec740ac + cb0bb8a commit 0f2c39f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

BLE_Host_Computer/ble_eval_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Connect to an "eval()" service over BLE UART.
2+
3+
from adafruit_ble import BLERadio
4+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
5+
from adafruit_ble.services.nordic import UARTService
6+
7+
ble = BLERadio()
8+
9+
uart_connection = None
10+
11+
while True:
12+
if not uart_connection:
13+
print("Trying to connect...")
14+
for adv in ble.start_scan(ProvideServicesAdvertisement):
15+
if UARTService in adv.services:
16+
uart_connection = ble.connect(adv)
17+
print("Connected")
18+
break
19+
ble.stop_scan()
20+
21+
if uart_connection and uart_connection.connected:
22+
uart_service = uart_connection[UARTService]
23+
while uart_connection.connected:
24+
s = input("Eval: ")
25+
uart_service.write(s.encode("utf-8"))
26+
uart_service.write(b'\n')
27+
print(uart_service.readline().decode("utf-8"))

BLE_Host_Computer/ble_eval_server.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Provide an "eval()" service over BLE UART.
2+
3+
from adafruit_ble import BLERadio
4+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
5+
from adafruit_ble.services.nordic import UARTService
6+
7+
ble = BLERadio()
8+
uart = UARTService()
9+
advertisement = ProvideServicesAdvertisement(uart)
10+
11+
while True:
12+
ble.start_advertising(advertisement)
13+
print("Waiting to connect")
14+
while not ble.connected:
15+
pass
16+
print("Connected")
17+
while ble.connected:
18+
s = uart.readline()
19+
if s:
20+
try:
21+
result = str(eval(s))
22+
except Exception as e:
23+
result = repr(e)
24+
uart.write(result.encode("utf-8"))

0 commit comments

Comments
 (0)