Skip to content

pb connect connects the hub chosen with pb use - #13

Merged
dlech merged 3 commits into
mainfrom
connect-honors-use
Nov 24, 2026
Merged

dlech merged 3 commits into
mainfrom
connect-honors-use

Conversation

@dlech

@dlech dlech commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

pb connect with no --hub went to whichever hub answered first, or to one
already connected, even with another hub chosen with pb use. Every other
command acts on the chosen hub, so a user ended up with one hub chosen
and a different one connected.

hub.connect with nothing named now goes to the chosen hub if there is
one; otherwise to one already connected; otherwise to the only hub
found. A chosen hub that is not found is named in the error, with the
name in its data too, and pb connect says how to choose another with
pb use.

Several hubs and nothing to say which is refused with the new
HUB_AMBIGUOUS, which lists them, rather than guessed at. That includes
several sharing the name asked for or chosen, as every hub flashed
without a name does. pb connect then asks which, in a terminal, and
says how to tell them apart without one.

  • Two ids on one transport are two hubs, and so are two names on any.
    One hub on a cable and over Bluetooth has an id each way, but the
    same name.
  • A hub only paired over RFCOMM, which lists the paired set switched on
    or not, counts and is chosen only when no other transport heard one,
    and pb connect does not offer it then either.
  • To hear a second hub at all, a search without an id listens for
    AMBIGUITY_SECONDS, 1 s, after the first hub heard, on USB as well. A
    City hub was measured advertising up to a second apart on Windows. A
    paired hub instead waits up to PAIRED_SECONDS, 3 s, for one to be
    heard, since Bluetooth takes a moment to start. Waiting on a
    permission dialog still moves the end of the search later.

A connected hub with the name asked for or chosen, or the connected one
when nothing is named, is that hub: a connected hub has stopped
advertising, so a search would pass it by and connect another hub with
the same name. It is handed back unless another transport is asked for,
and refused when exclusive. hub.connect says whether the hub was
connected already, as already_connected, and pb connect says "Already
connected" rather than claiming to have connected it.

PROTOCOL_VERSION and MIN_PROTOCOL_VERSION are 4: a client of 3 does not
know HUB_AMBIGUOUS. pb connect also escapes the hub names it prints
with rich markup, which "Connected to ..." did not before.

Tried on Windows with a Prime hub and a City hub, both named Pybricks
Hub: asked which with the name chosen, with nothing chosen and with
--hub, said Already connected for the connected one by choice and by
--hub while the other advertised, and named the chosen hub when both
were off.

Fixes pybricks/support#2866

A hub's type is in its scan response, as the PnP ID in service data, and
the scan response arrives after the advertisement. A hub reported on its
advertisement alone was listed without a type, and the picker showed it
as a row of its own. Seen on Windows with a City hub.

A sighting without a PnP ID is now passed over until one with it comes.
Every Pybricks firmware sends one, so a hub that still has not after
SCAN_RESPONSE_SECONDS has a firmware bug: it is logged, once per scan,
and not listed.

Tested with bleak's scanner replaced by one that replays advertisements.
@dlech
dlech force-pushed the connect-honors-use branch from 53284ec to fb91f1b Compare November 24, 2026 00:14
A real hub advertises only every so often, so one switched on beside
another may be heard a second after it. Simulated hubs were all heard
the moment a search began, which hides anything that decides too soon.

FakeHub.heard_after says how far into a search the hub is first heard.
It defaults to at once, so nothing that uses a simulated hub changes.
…hosen with pb use

pb connect with no --hub went to whichever hub answered first, or to one
already connected, even with another hub chosen with pb use. Every other
command acts on the chosen hub, so a user ended up with one hub chosen
and a different one connected.

hub.connect with nothing named now goes to the chosen hub if there is
one; otherwise to one already connected; otherwise to the only hub
found. A chosen hub that is not found is named in the error, with the
name in its data too, and pb connect says how to choose another with
pb use.

Several hubs and nothing to say which is refused with the new
HUB_AMBIGUOUS, which lists them, rather than guessed at. That includes
several sharing the name asked for or chosen, as every hub flashed
without a name does. pb connect then asks which, in a terminal, and
says how to tell them apart without one.

- Two ids on one transport are two hubs, and so are two names on any.
  One hub on a cable and over Bluetooth has an id each way, but the
  same name.
- A hub only paired over RFCOMM, which lists the paired set switched on
  or not, counts and is chosen only when no other transport heard one,
  and pb connect does not offer it then either.
- To hear a second hub at all, a search without an id listens for
  AMBIGUITY_SECONDS, 1 s, after the first hub heard, on USB as well. A
  City hub was measured advertising up to a second apart on Windows. A
  paired hub instead waits up to PAIRED_SECONDS, 3 s, for one to be
  heard, since Bluetooth takes a moment to start. Waiting on a
  permission dialog still moves the end of the search later.

A connected hub with the name asked for or chosen, or the connected one
when nothing is named, is that hub: a connected hub has stopped
advertising, so a search would pass it by and connect another hub with
the same name. It is handed back unless another transport is asked for,
and refused when exclusive. hub.connect says whether the hub was
connected already, as already_connected, and pb connect says "Already
connected" rather than claiming to have connected it.

PROTOCOL_VERSION and MIN_PROTOCOL_VERSION are 4: a client of 3 does not
know HUB_AMBIGUOUS. pb connect also escapes the hub names it prints
with rich markup, which "Connected to ..." did not before.

Tried on Windows with a Prime hub and a City hub, both named Pybricks
Hub: asked which with the name chosen, with nothing chosen and with
--hub, said Already connected for the connected one by choice and by
--hub while the other advertised, and named the chosen hub when both
were off.

Fixes: pybricks/support#2866
@dlech
dlech force-pushed the connect-honors-use branch from cf333fd to 21bad7a Compare November 24, 2026 01:38
@dlech
dlech merged commit d198f49 into main Nov 24, 2026
23 checks passed
@dlech
dlech deleted the connect-honors-use branch November 24, 2026 01:44
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.

[Bug] 'pb connect' does not use the hub selected by 'pb use'

1 participant