On the MatrixPortal, I have installed the ble_uart_echo_test.py. I have made changes to the code based on other MatrixPortal CircuitPython examples in order to get ble on the hardware. This code works when I use the Apple iPhone BLE "Bluefruit Connect" app. I can send messages and they are echoed back as expected.
ble_uart_echo_test.py
Code: Select all
# 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.
"""
import time
import board
import json
import displayio
import rgbmatrix
import framebufferio
from adafruit_airlift.esp32 import ESP32
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
# --- Display setup ---
displayio.release_displays()
matrix = rgbmatrix.RGBMatrix(
width=64, bit_depth=4,
rgb_pins=[
board.MTX_R1,
board.MTX_G1,
board.MTX_B1,
board.MTX_R2,
board.MTX_G2,
board.MTX_B2
],
addr_pins=[
board.MTX_ADDRA,
board.MTX_ADDRB,
board.MTX_ADDRC,
board.MTX_ADDRD
],
clock_pin=board.MTX_CLK,
latch_pin=board.MTX_LAT,
output_enable_pin=board.MTX_OE
)
display = framebufferio.FramebufferDisplay(matrix)
esp32 = ESP32()
adapter = esp32.start_bluetooth()
ble = BLERadio(adapter)
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
print(f'{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)
ble_uart_echo_client.py
Code: Select all
# 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()
Is there something else I need to do in the server side code in order for the client to locate and use the BLEUARTService?
Alternatively, is there another Adafruit device with buttons and a little TFT that I can use with BLE central code?
--
mak3r