BLE UARTService client just hangs

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mak3ra
 
Posts: 1
Joined: Wed Mar 13, 2019 10:35 am

BLE UARTService client just hangs

Post by mak3ra »

I have 2 adafruit devices which I would like to connect to each other via BLE. The first device is the MatrixPortal M4 and the second is the CLUE. What I am trying to do is setup the CLUE so that I can control what is displayed on the MatrixPortal using the CLUE buttons. I was able to reproduce the issue I'm having with one of the BLE examples which I'll explain below.

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)
On the CLUE, I have installed the ble_uart_echo_client.py without changes. This code fails at line 17 where is just hangs up with no errors or exceptions. It seems that the `ble.connections` object just goes into some infinite loop when I try to identify `UARTService` connections. The CLUE locks up and I either need to reset it or stop the echo program running on the MatrixPortal to force disconnect.

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()
I have also tried using the arduino example `central_bleuart.ino` on the CLUE but that also fails due to the `conn_handle` passed into the `connect_callback()` having no value. At least in the arduino code it is fault tolerant insofar as it disconnects when the issue occurs and it retries. Of course, it never actually finds the uart service so it just connects/disconnects ad infinitum.

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

Locked
Please be positive and constructive with your questions and comments.

Return to “CLUE Board”