Skip to content

[Feature] Toggle Bluetooth with Bluetooth button. #1615

Closed

Description

@laurensvalk

We are planning to use Pybricks for the WRO RoboMission.

Having bluetooth enabled permanently is problematic for competition purposes. The rules (Section 5.11 in https://wro-association.org/wp-content/uploads/WRO-2023-RoboMission-General-Rules.pdf) cause some inconveniences and there is the general problem that a connection is easy to hijack which can be used to ruin a run of a competitor. While that is forbidden it would be very difficult to pinpoint an attacker.

The cleanest way to resolve this would be to disable bluetooth (at least temporarily). It seems to me that this is one of the few things which are actually better with the lego firmeware which only enables bluetooth after the dedicated hardware button is pressed.

Originally posted by @DrTom in #1089

Activity

  1. laurensvalk commented on Apr 28, 2024

    @laurensvalk
    MemberAuthor
  2. self-assigned this
    on Apr 28, 2024
  3. laurensvalk commented on Apr 28, 2024

    @laurensvalk
    MemberAuthor

    Some ideas for the user experience:

    • Default behavior: Bluetooth on with every boot, same as now.
    • Bluetooth can only be toggled while the user program is not running. Otherwise the button could not be used in user programs.
    • Bluetooth can only be toggled while the robot is not connected to the computer. (Open question. Thoughts? Or should pressing it forcefully disconnect?)

    Suggestions for color indications:

    • Blue: Bluetooth on/active.
    • Red: Bluetooth off/inactive**

    ** Thoughts? I was thinking a red color is more explicit than off, more easily communicated to judges, preventing cheating by using an older firmware. But if you say light off is better, we could do that.


    Scope: only for SPIKE Prime and Robot Inventor? Is this also needed for SPIKE Essential?


    Alternate ideas, Bluetooth on for coding, but no RC

    If I'm understanding it right, the above has no technical utility, but it's to satisfy robot competition rules.

    If that's the case, would it make sense to allow coding, but just disable all in-program Bluetooth features like remote control and broadcasting/observing?

  4. added
    software: pybricks-micropython
    topic: bluetooth
    on Apr 28, 2024
  5. ggramlich commented on Apr 28, 2024

    @ggramlich

    As @laurensvalk asked for feedback:

    This video "WRO Robo-Check" is provided to inform the German judges.
    https://www.youtube.com/watch?v=xQGuZWgKVKo
    If you want to optimize for WRO compatibility, it would be best to resemble this behavior:

    • After Boot: Bluetooth is off (this also has the advantage that nobody could steal the connection without physical access to the robot), the light could be off or red - I would prefer off.
    • Toggle on to accept connection by long-press, then blink blue
    • After connection is established, light is constantly blue

    I do understand, that in a home setting, turning bluetooth on automatically is convenient, but actually as you still have to press the connect button in the Pybricks software on the computer and could show some message there (as you already do right now, if no device is found / the user cancels). The message could ask you to switch on the robot and long-press the bluetooth button (and still provide help to install the firmware). It is not a big deal to force the user to do another button press on the robot. Every user switching from the Lego software is already used to that. And once you are connected, you stay connected.

    Alternate ideas, Bluetooth on for coding, but no RC

    The main reason for this rule is NOT that they want to prevent remote controlling the robot, but to prevent that you might switch the program.
    There is a randomization for each run in the competition and for some objects you don't know where they are placed. The program is supposed to handle all of these situations by recognizing them (or possibly have some mechanical measures that work for all cases). There is a countdown and the robot has to be placed on the parking area before the countdown runs out with the program for the next run on it. This is where the robots should be turned off or have Bluetooth disabled. After the robot check, the actual randomisation is revealed and this is the time where a cheating team might change the program on their robot (or a malicious team might delete the program on your robot).

    So "Bluetooth on for coding, but no RC" is not an option.

    Scope: only for SPIKE Prime and Robot Inventor? Is this also needed for SPIKE Essential?

    I personally would not care about Spike Essential, I have never seen them in a competition.

    Bluetooth can only be toggled while the robot is not connected to the computer. (Open question. Thoughts? Or should pressing it forcefully disconnect?)

    That would be fine, maybe disconnecting from the computer should simply lead to the state "Bluetooth off".

  6. nkarstens commented on Apr 28, 2024

    @nkarstens

    I think the best experience will be to save the setting to persistent storage. The kids can program it for the months leading to the competition without forgetting to enable it, and then they can take it to competitions without worrying about forgetting to disable it.

    My preference would be red = off and blue = on. With this approach it will be hardest to make a mistake and not notice that you are in the wrong state. I'd be fine with no light = off as well, if others feel strongly.

  7. afarago commented on Apr 28, 2024

    @afarago

    In the meantime I have been to some WRO competitions either as a coach or in some countries I have the honor to be head judging the WRO competition.

    As for the SPIKE/RI sets Bluetooth is always on even with the stock firmware, therefore turning bluetooth off is not really an immediate need anymore in my opinion.

    There are a few points for best competition experience left still, some connected with BT.

    • additional BT authentication step plus remember BT connected devices/last connected device (similarly to SPIKE or EV3 stock firmware)
      a) idea for implementation: SPIKE is only discoverable for a short time (i.e. 30 sec) when e.g. BT button was long pressed, other than that BT is on, but cannot be connected by new devices
      b) any previously connected device (or for sake of simplification the last connected device) can connect as today
    • animation misleading judges - when device is not connected the main button is animating in blue - giving pybricks-unaware judges that either a program is running already or BT is connected -- no big idea from my side how to resolve this, probably teams need to explain it to the judges every time
    • as a judge I would highly appreciate to see a BT connection indication similar to the stock firmware (BT button blue light on) - as today I have no real means to check if the brick (with pybricks) is really not connected and RC controlled
  8. laurensvalk commented on Apr 29, 2024

    @laurensvalk
    MemberAuthor

    Thanks everyone for the input! Combing all that with the base suggestion, it seems like the following gets quite close to most what you'd need:

    • Bluetooth is on by default, same as now (no breaking change).
    • Toggle Bluetooth on/off with button.
    • Bluetooth light: blue is on, red is off.
      We can reserve other colors for other future behaviors, if we ever want them (e.g green = coding but no RC).
    • Make it persistent after reboot. So you can achieve off-on-boot, for judge review. Judges also see the visual indicator.
    • Spike Prime / Robot Inventor only.

    Regarding suggestions such as long-press / additional BT authentication step / enabling discovery, I think we would rather avoid those, especially since you could now achieve off-on-boot as above. Already, discoverability is disabled once a connection is established.

    I think bluetooth on/off with a simple button/light is easier to understand for kids.

    I'm not a big fan of the separate discoverability button press in the official app. In my opinion that's too many different clicks, mouse moves, and buttons on the hub just to establish a connection.

  9. m7thon commented on Apr 30, 2024

    @m7thon
    • Bluetooth is on by default, same as now (no breaking change).
    • Toggle Bluetooth on/off with button.
    • Bluetooth light: blue is on, red is off.
      We can reserve other colors for other future behaviors, if we ever want them (e.g green = coding but no RC).
    • Make it persistent after reboot. So you can achieve off-on-boot, for judge review. Judges also see the visual indicator.
    • Spike Prime / Robot Inventor only.

    I used the branch from Nate Karstens implementing exactly this behavior with two WRO teams in the preparation and the event, and this was easy and intuitive for the kids to use (and I believe this fulfills the WRO rules).

  10. nkarstens commented on Apr 30, 2024

    @nkarstens

    To be clear, pybricks/pybricks-micropython#195 does not support persistent settings yet.

    @laurensvalk in the discussion on the PR you mentioned that there were still some things that you were figuring out regarding persistent settings. Do you have any more information on that?

  11. laurensvalk commented on May 3, 2024

    @laurensvalk
    MemberAuthor

    there were still some things that you were figuring out regarding persistent settings. Do you have any more information on that?

    I've opened an issue for it in #1622

  12. afarago commented on May 4, 2024

    @afarago

    Could we consider having 3 BT modes?

    1. BT on open (current)
    2. BT off
    3. BT on secure - only connect to last connected MAC (after mode 1) - needs persistent storage for MAC [Feature] Reserve memory region for persistent system settings. #1622

    Though kids are nice it would be a disaster if during the competition anyone would connect to your hub when doing the programming / testing part.

    (You know the analogy - in the IoT abbreviation the letter "s" stands for security)

  13. laurensvalk commented on Jun 4, 2024

    @laurensvalk
    MemberAuthor

    pybricks/pybricks-micropython#247 implements the following. See the rationale in that PR for some of the technical details.

    This could serve as a practical starting point for (teams in need) now, but I'm open to reconsidering the UI on Prime Hub now that we have this, possibly deviating from the other hubs. Maybe it's the Bluetooth light that should blink, for example.

    The setting is not persistent for now, but this should probably not hold back the rest of it.

    image

    toggle.mp4
  14. 14 remaining items

  15. BertLindeman commented on Jun 5, 2024

    @BertLindeman

    @BertLindeman - I've updated the build in case you want to try it.

    Was expecting a link to the build, but I assume you mean build 3380.
    If I hurry I can just do it before I go assist on a course "HELP, I have a smartphone" 😄

  16. BertLindeman commented on Jun 5, 2024

    @BertLindeman

    Did a quick test and did not see the previous problem.

    Thought I saw something strange but the same test on RobotInventor at 3.5.0 does the same.

    For the curious
    • run a program (for me: tools try_movehub_on_prime.py)
    • normal stop from the app and disconnect from the app
    • stop the hub so the program gets saved
    • without (re)connecting to the app start the program (button press)
    • within seconds the hub shuts down on it's own; Battery is at voltage: 6.955 V
      As said both this build 3380 prime and 3.5.0 RobotInvenotor do it.
      Program is garbage fiddling to get movehub randint with a lot of printing.
      Will try later to get a smaller program. But that is another issue then.
  17. laurensvalk commented on Jun 6, 2024

    @laurensvalk
    MemberAuthor

    Work in progress demo with persistency after reboot:

    persistent.mp4
  18. laurensvalk commented on Jun 7, 2024

    @laurensvalk
    MemberAuthor
    • Bluetooth is on by default, same as now (no breaking change).
    • Toggle Bluetooth on/off with button.
    • Bluetooth light: blue is on, red is off.
      We can reserve other colors for other future behaviors, if we ever want them (e.g green = coding but no RC).
    • Make it persistent after reboot. So you can achieve off-on-boot, for judge review. Judges also see the visual indicator.
    • Spike Prime / Robot Inventor only.

    This has now been implemented! Thanks everyone for pushing for this.

    If you want to try it out before we release a new beta, just follow these instructions. That page includes a link to the latest build.

  19. deleted a comment from BertLindeman on Jun 26, 2024
  20. afarago commented on Jul 1, 2024

    @afarago

    Is this normal that every time I toggle the bluetooth off and on I see a "new" device witha seemingly random MAC address?

    At the moment as due to browser bluetooth API limitation there is no autoconnect - it does not make a difference, yet seems odd.

    image

  21. dlech commented on Jul 1, 2024

    @dlech
    Member

    Yes, we had to do that to work around issues with OS Bluetooth stacks caching the GATT attributes that change when the firmware is changed. Things just work better when it is acting like it is connecting to a new device every time.

  22. afarago commented on Jul 11, 2024

    @afarago

    Yes, we had to do that to work around issues with OS Bluetooth stacks caching the GATT attributes that change when the firmware is changed. Things just work better when it is acting like it is connecting to a new device every time.

    Is there any chance to reconsider this?

    Checking the web bluetooth developments we already have several routes to reconnect to a previously allowed device. This would significantly improve the user experience of pybricks code IDE.

    I have played around with the code and checked this approach using navigator.bluetooth.getDevices() - it seemed quite flawless from IDE perspective yielding to a potentially small amount of changes.

  23. laurensvalk commented on Jul 11, 2024

    @laurensvalk
    MemberAuthor

    Yes, we had to do that to work around issues with OS Bluetooth stacks caching the GATT attributes that change when the firmware is changed. Things just work better when it is acting like it is connecting to a new device every time.

    Is there any chance to reconsider this?

    Maybe when pybricks/pybricks-micropython#246 is a bit more polished, Bluetooth is probably as complete as it gets. So if we're happy with all the Bluetooth features and agree not to change them anymore, I suppose we could consider that again at some point.

  24. dlech commented on Jul 11, 2024

    @dlech
    Member

    This issue is closed, so I would highly recommend to start a new issue so that this discussion does not get lost and forgotten.

  25. zoligyenge commented on Oct 2, 2024

    @zoligyenge

    Hello the latest page link appears broken. Could you please post the new link to the latest builds?
    Thanks

    If you want to try it out before we release a new beta, just follow these instructions. That page includes a link to the latest build.

  26. added theissue type on Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

software: pybricks-micropythontopic: bluetooth

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions