Initial commit
This commit is contained in:
39
btproxy/ble_uart_echo_client.py
Normal file
39
btproxy/ble_uart_echo_client.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
"""
|
||||
Used with ble_uart_echo_test.py. Transmits "echo" to the UARTService and receives it back.
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
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
|
||||
print("echo")
|
||||
uart = connection[UARTService]
|
||||
uart.write(b"echo")
|
||||
# Returns b'' if nothing was read.
|
||||
one_byte = uart.read(4)
|
||||
if one_byte:
|
||||
print(one_byte)
|
||||
print()
|
||||
time.sleep(1)
|
||||
|
||||
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()
|
27
btproxy/ble_uart_echo_test.py
Normal file
27
btproxy/ble_uart_echo_test.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
"""
|
||||
Can be used with ble_uart_echo_client.py or with the UART page on the
|
||||
Adafruit Bluefruit Connect app. Receives characters from the UARTService
|
||||
and transmits them back.
|
||||
"""
|
||||
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
|
||||
ble = BLERadio()
|
||||
uart = UARTService()
|
||||
advertisement = ProvideServicesAdvertisement(uart)
|
||||
|
||||
while True:
|
||||
ble.start_advertising(advertisement)
|
||||
while not ble.connected:
|
||||
pass
|
||||
while ble.connected:
|
||||
# Returns b'' if nothing was read.
|
||||
one_byte = uart.read(1)
|
||||
if one_byte:
|
||||
print(one_byte)
|
||||
#uart.write(one_byte)
|
43
btproxy/main.py
Normal file
43
btproxy/main.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import time
|
||||
|
||||
import bleak
|
||||
from adafruit_ble import BLERadio
|
||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||
from adafruit_ble.services.nordic import UARTService
|
||||
|
||||
ble = BLERadio()
|
||||
|
||||
def scan():
|
||||
print("disconnected, scanning")
|
||||
for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
|
||||
if not advertisement.complete_name.startswith('Feather'):
|
||||
continue
|
||||
|
||||
ble.connect(advertisement)
|
||||
print("connected to", advertisement.complete_name)
|
||||
break
|
||||
ble.stop_scan()
|
||||
|
||||
def read(connection):
|
||||
uart = connection[UARTService]
|
||||
if uart.in_waiting:
|
||||
res = uart.read(uart.in_waiting)
|
||||
print(res)
|
||||
|
||||
def loop():
|
||||
while True:
|
||||
while ble.connected:
|
||||
for connection in ble.connections:
|
||||
try:
|
||||
read(connection)
|
||||
except bleak.exc.BleakError:
|
||||
print('Disconnected, waiting 3 seconds and trying to reconnect...')
|
||||
connection.disconnect()
|
||||
time.sleep(3)
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
scan()
|
||||
|
||||
if __name__ == '__main__':
|
||||
loop()
|
26
btproxy/print_uart.py
Normal file
26
btproxy/print_uart.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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()
|
Reference in New Issue
Block a user