Skip to content

ble not staying paired upon reset/reconnects. #10739

@alpoman

Description

@alpoman

CircuitPython version and board name

cp 10.0.3
waveshare esp32-s3 touch lcd-2

Code/REPL

# code.py -- ESP32-S3 Reverse TFT Feather
# Minimal BLE HID keyboard: D0, D1, D2 send A, B, C

import time
import board
import digitalio

import adafruit_ble
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# -----------------------------
# Buttons on ESP32-S3 Reverse TFT
# -----------------------------
# Per guide:
#  - D0 uses Pull.UP -> active LOW when pressed
#  - D1, D2 use Pull.DOWN -> active HIGH when pressed  [oai_citation:2‡Adafruit Learning System](https://learn.adafruit.com/esp32-s3-reverse-tft-feather?view=all&utm_source=chatgpt.com)

button0 = digitalio.DigitalInOut(board.D0)
button0.switch_to_input(pull=digitalio.Pull.UP)

button1 = digitalio.DigitalInOut(board.D1)
button1.switch_to_input(pull=digitalio.Pull.DOWN)

button2 = digitalio.DigitalInOut(board.D2)
button2.switch_to_input(pull=digitalio.Pull.DOWN)

prev0 = button0.value
prev1 = button1.value
prev2 = button2.value

# -----------------------------
# BLE HID setup (minimal)
# -----------------------------
ble = adafruit_ble.BLERadio()
ble.name = "RevTFT HID"

hid = HIDService()
advertisement = ProvideServicesAdvertisement(hid)
advertisement.short_name = "RevTFT"  # keep adv payload small
# You can add: advertisement.appearance = 961  # keyboard icon
# but skip it at first if you're still hitting memory issues

kbd = Keyboard(hid.devices)

print("BLE HID starting; device name:", ble.name)

while True:
    # Start advertising and wait for a connection
    print("Advertising...")
    ble.start_advertising(advertisement)

    while not ble.connected:
        # Just idle while waiting for a connection
        time.sleep(0.05)

    print("Connected!")

    # Now we're connected: scan buttons and send keys
    while ble.connected:
        v0 = button0.value
        v1 = button1.value
        v2 = button2.value

        # Helper: send a key once on the press edge
        def send_key(keycode, label):
            try:
                kbd.send(keycode)
                print(f"{label} key sent")
            except OSError as e:
                # connection might have dropped mid-send
                print("Send error:", e)

        # D0: active LOW -> press edge = high -> low
        if prev0 and not v0:
            send_key(Keycode.A, "D0 (A)")

        # D1: active HIGH -> press edge = low -> high
        if not prev1 and v1:
            send_key(Keycode.B, "D1 (B)")

        # D2: active HIGH -> press edge = low -> high
        if not prev2 and v2:
            send_key(Keycode.C, "D2 (C)")

        prev0 = v0
        prev1 = v1
        prev2 = v2

        time.sleep(0.01)  # debounce

    print("Disconnected; will re-advertise")
    # Loop repeats, re-calling start_advertising()

Behavior

after a few days working with esp32-s3 and ble hid(and midi). i noticed that if i forget settings in windows and clear binding on esp, i can get a stable ble connection, but, if i turn off ble in windows( or drop the connection in anyway). it may reconnect, but no longer works. also, once power cycle of device happens, any pairing still shows, but device never reconnects and works again, unitil i forget ble settings again, unbind device, reboot, and re-pairing then works again. obviously this isnt correct, as it shoukd survive reboots and signal drops/reconnects. I noticed as soon as i pkug in device, windows shows esp device as available bluetooth even before any code runs... is there a ble stack in the tinybootloader? that may be causing the conflict, and causing problems with ble resources/ mismatches while trying to reconnect. i dont know much how the bootloader, u2f file is configured for this board, and how its ble may or may not be in some kind ofauto mode on boot. this behavior is exactly same on ipad and mac as well, just curious if any headway on this or info is available, i just wanted to add my observations. btw, i was testing using Mikey's stripped down ble hid test code from this post, which avoids the nimbe memory bug. one more note, after once working, and powercycle, the code tryies to run but hangs...doesnt crash...and if i turn off blutooth in windows, code resumes. and if i turn on blutooth again, it hangs again. its like the esp is blocking on trying to connect, very odd. thanks for any help,
andy

Description

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions