Discuss Scratch
- Discussion Forums
- » Connecting to the Physical World
- » Lego Boost & Web Bluetooth API
- tomtom2009
-
New Scratcher
8 posts
Lego Boost & Web Bluetooth API
Hello,
Based on WEDO 2.0 extension and the excellent work of Andrey Pokhilko (python interface for lego Boost), I developed an extension for Scratch 3.0 that allows to handle most of the functionalities of Lego Boost (motors, colors detector, …).
Because I wanted to use the bluetooth of my computer instead of the “scratch link” and the USB-BLE adapter, I also wrote a new io interface that manage the recent “Web Bluetooth API” (https://www.chromestatus.com/feature/5264933985976320) that is now included in recent version of chrome (and firefox, I think).
I wrote the extension by directly modifying the scratch 3.0 source code.
Of course it remains some works :
- Most of all, perform more tests on the Lego Boost and debug the code
- Check my code, I am not a javascript developer
- More tests on WEBBLE
- Write specific error message for WEBBLE (for the moment, because it is just an adaptation of io/ble.js, the messages are the ones of “scratch link” and therefore not consistent with WEBBLE)
- Allows to choose either WEBBLE or “Scratch Link”
If you think my piece of code is interesting enough I would like to share it. But because, scratchx is not yet available for scratch 3.0, what is the best way to do it?
Julien
Based on WEDO 2.0 extension and the excellent work of Andrey Pokhilko (python interface for lego Boost), I developed an extension for Scratch 3.0 that allows to handle most of the functionalities of Lego Boost (motors, colors detector, …).
Because I wanted to use the bluetooth of my computer instead of the “scratch link” and the USB-BLE adapter, I also wrote a new io interface that manage the recent “Web Bluetooth API” (https://www.chromestatus.com/feature/5264933985976320) that is now included in recent version of chrome (and firefox, I think).
I wrote the extension by directly modifying the scratch 3.0 source code.
Of course it remains some works :
- Most of all, perform more tests on the Lego Boost and debug the code
- Check my code, I am not a javascript developer
- More tests on WEBBLE
- Write specific error message for WEBBLE (for the moment, because it is just an adaptation of io/ble.js, the messages are the ones of “scratch link” and therefore not consistent with WEBBLE)
- Allows to choose either WEBBLE or “Scratch Link”
If you think my piece of code is interesting enough I would like to share it. But because, scratchx is not yet available for scratch 3.0, what is the best way to do it?
Julien
- SimpleScratch
-
Scratcher
500+ posts
Lego Boost & Web Bluetooth API
I would like to share it. But because, scratchx is not yet available for scratch 3.0, what is the best way to do it?
At them moment, only method I know of is to publish your code changes as a fork on Github, that interested people could use npm to load and have a look and play with it
- tomtom2009
-
New Scratcher
8 posts
Lego Boost & Web Bluetooth API
OK. Thank you for the advise.
So, I created 2 forks of scratch3 on github:
1 - https://github.com/m8hpw/scratch-gui
2 - https://github.com/m8hpw/scratch-vm
Both branches are called “boost_and_webble”
I am new at github, so please do not hesitate to contact me if something does not work.
So if you want to try.
1 - clone scratch-gui (“boost_and_webble” branche) and scratch-vm (“boost_and_webble” branche) in the same folder
2 - cd scratch-gui
3 - npm start
4 - wait for compilation, upload , etc
5 - stop (ctrl-c)
4 - rename scratch-gui/node_modules/scratch-vm in scratch-vm.old
5 - create a symbolic link of scratch-vm folder to scratch-gui/node_modules/scratch-vm.
6 - npm start
I only tested the code on linux, with a recent version of chrome. You may have to activate bluetooth support (see Before we start
section at https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web)
Scratch should be run under https protocole, otherwise bluetooth won't work (it is a security issue).
So, I created 2 forks of scratch3 on github:
1 - https://github.com/m8hpw/scratch-gui
2 - https://github.com/m8hpw/scratch-vm
Both branches are called “boost_and_webble”
I am new at github, so please do not hesitate to contact me if something does not work.
So if you want to try.
1 - clone scratch-gui (“boost_and_webble” branche) and scratch-vm (“boost_and_webble” branche) in the same folder
2 - cd scratch-gui
3 - npm start
4 - wait for compilation, upload , etc
5 - stop (ctrl-c)
4 - rename scratch-gui/node_modules/scratch-vm in scratch-vm.old
5 - create a symbolic link of scratch-vm folder to scratch-gui/node_modules/scratch-vm.
6 - npm start
I only tested the code on linux, with a recent version of chrome. You may have to activate bluetooth support (see Before we start
section at https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web)
Scratch should be run under https protocole, otherwise bluetooth won't work (it is a security issue).
- tomtom2009
-
New Scratcher
8 posts
Lego Boost & Web Bluetooth API
If you want to test the Lego Boost extension on a linux computer,
First you should install some packages
sudo apt-get install nodejs npm
and then copy the git files, install, etc.
mkdir scratch_boot
cd scratch_boot
git clone –single-branch –branch boost_and_webble https://github.com/m8hpw/scratch-gui.git
git clone –single-branch –branch boost_and_webble https://github.com/m8hpw/scratch-vm.git
cd scratch-vm
npm install
cd ..
cd scratch-gui
npm install
cd node_modules
ln -s ../../scratch-vm scratch-vm
cd ..
npm start
You should see a message saying to
Project is running at https://0.0.0.0:8601/
Copy past this address into chrome. You should accept to pass the security issue (the certificate is not recognisz as an official one)
First you should install some packages
sudo apt-get install nodejs npm
and then copy the git files, install, etc.
mkdir scratch_boot
cd scratch_boot
git clone –single-branch –branch boost_and_webble https://github.com/m8hpw/scratch-gui.git
git clone –single-branch –branch boost_and_webble https://github.com/m8hpw/scratch-vm.git
cd scratch-vm
npm install
cd ..
cd scratch-gui
npm install
cd node_modules
ln -s ../../scratch-vm scratch-vm
cd ..
npm start
You should see a message saying to
Project is running at https://0.0.0.0:8601/
Copy past this address into chrome. You should accept to pass the security issue (the certificate is not recognisz as an official one)
Last edited by tomtom2009 (Feb. 13, 2019 19:14:10)
- GrumpyOldMM
-
Scratcher
1 post
Lego Boost & Web Bluetooth API
This is great, thanks!
I can confirm that your instructions also work on a Mac.
I can confirm that your instructions also work on a Mac.

- Alessandro999
-
Scratcher
1 post
Lego Boost & Web Bluetooth API
I am sorry but I don't get it to work on my Macbook. I downloaded the two Boost & Webble branches from GitHub and ran the commands mentioned in the post above. Scratch is running under https://0.0.0.0:8601/ as expected. When I click on the extension button, I can see a “Lego Boost” extension. When I click on it, nothing happens (no additional commands or Blocks are added to the list). There are several other extensions listed such as “Lego Mindstorms EV3” which work (an additional block with new commands appears).
Actually, I only want to use Bluetooth (no Lego Boost). What do I need to do to get it to work? Thanks a lot!
Actually, I only want to use Bluetooth (no Lego Boost). What do I need to do to get it to work? Thanks a lot!
- tomtom2009
-
New Scratcher
8 posts
Lego Boost & Web Bluetooth API
Actually, as I wrote the code, integrated bluetooth only works for Lego Boost. The other extension such as EV3 still use “scratch link”.
I think that it would be great if Scratch can propose to use either “scratch link” or integrated buetooth. But it requires a relatively important amount a coding work.
I think that it would be great if Scratch can propose to use either “scratch link” or integrated buetooth. But it requires a relatively important amount a coding work.
- MasterOfGizmo
-
New Scratcher
20 posts
Lego Boost & Web Bluetooth API
I second this. This is a very similar approach to my WebUSB based extension for the ftDuino. It really makes sense to use the builtin APIs of those modern browsers.
Using WebUSB makes my extension even run on Android devices connecting the ftDuino via a USB OTG host cable directly to the phone or tablet. I'd assume that using WebBluetooth has similar advantages.
One problem i am facing is that many of these hardware related functions must be called from the user input (probably to prevent some web pages from automatically messing with your hatrdware). So some nice entry points for this in the extensions interface would be nice. I especially miss this traffic lights like thing that e.g. ScratchX has. Something like that working as a “broken”, “diconnected”, “connected” icon in the toolbar and which i could use to report and change the connection state would be awsome.
Using WebUSB makes my extension even run on Android devices connecting the ftDuino via a USB OTG host cable directly to the phone or tablet. I'd assume that using WebBluetooth has similar advantages.
One problem i am facing is that many of these hardware related functions must be called from the user input (probably to prevent some web pages from automatically messing with your hatrdware). So some nice entry points for this in the extensions interface would be nice. I especially miss this traffic lights like thing that e.g. ScratchX has. Something like that working as a “broken”, “diconnected”, “connected” icon in the toolbar and which i could use to report and change the connection state would be awsome.
- MasterOfGizmo
-
New Scratcher
20 posts
Lego Boost & Web Bluetooth API
BTW: Publishing the result so other can test it is rather simple. Just do a “npm run build” and put the resulting build directory on any web server.
- kevsterino
-
Scratcher
15 posts
Lego Boost & Web Bluetooth API
The main challenge in using something like Web Bluetooth is that it doesn't, and perhaps never will, work across all the major devices, platforms, and browsers. Several companies have expressed concerns with the general concept of security and privacy when connecting to BLE-devices through your browser. You can see an overview of current support for Web Bluetooth here:
https://caniuse.com/#feat=web-bluetooth
https://caniuse.com/#feat=web-bluetooth
- Discussion Forums
- » Connecting to the Physical World
-
» Lego Boost & Web Bluetooth API