Skip to content

pbio/drv/bluetooth_nxt: Fix reconnecting. - #516

Open
fuzic wants to merge 3 commits into
pybricks:masterfrom
fuzic:nxt-bt-reject
Open

fuzic wants to merge 3 commits into
pybricks:masterfrom
fuzic:nxt-bt-reject

Conversation

@fuzic

@fuzic fuzic commented Oct 8, 2026

Copy link
Copy Markdown

Fixes Bluetooth on NXT getting stuck after a disconnect:

  • Dropping a host that doesn't speak Pybricks (e.g. the macOS Bluetooth panel) shut down Bluetooth until a restart. Now it goes back to listening.
  • If the host disconnects while the hub is sending, the BC4 stops taking data. stdout then stalled on all transports, USB included. Now the link is dropped after 1 s.
  • The BC4 was reset after every connection, which made macOS fail to reconnect, and each failure caused another reset. It's now only reset when it stops responding.

Known limitation: disconnecting mid-transfer still needs a BC4 reset, after which macOS fails the first reconnect attempt. Unsubscribing before closing avoids this.

Tested on hardware with a macOS host.

fuzic added 3 commits October 8, 2026 09:40
The serial driver drops RFCOMM hosts that don't send a Pybricks message
within 5 seconds (e.g. the macOS Bluetooth panel, which opens the port
when connecting). On NXT, this disconnect also stopped the Bluetooth
process, which was only meant to happen on shutdown. Nothing could
connect after that until the hub was restarted.

Drop just the connection and go back to listening, as on BTstack. A
shutdown still waits for the connection to close.
If the host closes the connection while the hub is sending (e.g. stdout
at the end of a program), the BC4 can stop taking data without dropping
the stream. The transmission then never finishes, which stalls stdout on
all transports and leaves the BC4 unable to accept a new connection.

Give up after 1 second, as the USB driver does after 500 ms, and drop
the link. The BC4 does not take commands in this state, so go straight
to the reset that restarts it instead of closing the connection first.
The BC4 was reset after every connection. This drops the radio link
without telling the host, which then fails to connect again for a while
(e.g. macOS keeps trying the old link). Each failed attempt ended in
another reset, so reconnecting could fail indefinitely.

Once the BC4 reports the connection closed, wait for the next one
without a reset. Only reset it if it stops responding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant