The micro:bit uses Bluetooth Low Energy, a power-friendly version of Bluetooth technology that allows for wireless communication between smartphones and tablets, allowing for seamless connection to the Internet of things. This chapter presents some of the basic things that you can do with Bluetooth Low Energy.
13-1. Adding Bluetooth Services Extension
Problem
You want to add the Bluetooth Services extension to the MakeCode editor.
Solution
In the Toolbox, click on Advanced to expand the package list. Now, scroll down the package list and click on Extensions.
- In the Extensions page, click on the bluetooth (Bluetooth services) (Figure 13-1 ).
- In the Some extensions will be removed window, click Remove extension(s) and add bluetooth button (Figure 13-2 ).
How It Works
Bluetooth extension allows device like a smartphone to use any of the Bluetooth “services” that the micro:bit has. If you want to use the features of the Bluetooth extension, it must first be paired with the micro:bit.
Once enabled, the extension can be found in the Toolbox and ready for access. The Bluetooth extension is incompatible with the radio, radio-broadcast, and NeoPixel extensions. You must first remove these extensions to add the Bluetooth extension.
13-2. Pairing Your micro:bit
Problem
You want to pair your micro:bit with your smartphone or tablet using Bluetooth.
Solution
- Go to Google Play Store and search for the BBC micro:bit. From the search result, choose the official micro:bit app (Figure 13-3 ).
- Choose INSTALL to install the app on your Android smartphone or tablet (Figure 13-4 ).
After installed, open the app by choosing the OPEN button.
From the micro:bit app, select the CONNECT button.
In the Connect page, select the PAIR A NEW MICRO:BIT button.
In STEP 1, on the micro:bit, hold down the button A and B on the front of the board and reset button on the back of the board for 3 seconds and then release the reset button.
The micro:bit display will fill up and display the Bluetooth logo to indicate that it has entered the pairing mode.
Select NEXT on the Android device, and copy the pattern that is displayed on the micro:bit into the micro:bit app.
In STEP 2, Select PAIR on the Android device to search for the micro:bit.
If the pairing is successful, you will get a message on the screen and a tick on the micro:bit.
Finally, press the reset button on the micro:bit to complete the pairing process.
Go to the iTunes app store and search for the BBC micro:bit.
Then install the official micro:bit app; on your iPhone or iPad, Open the app.
From the micro:bit app, select Choose micro:bit.
In the Choose micro:bit page, select Pair a new micro:bit.
On the micro:bit, hold down the button A and B on the front of the board and reset button on the back of the board for 3 seconds and then release the reset button.
The micro:bit display will fill up and display the Bluetooth logo to indicate that it has entered the pairing mode.
Select NEXT on the iOS device, and copy the pattern that is displayed on the micro:bit into the micro:bit app.
In STEP 2, Select PAIR on the iOS device to search for the micro:bit.
If the pairing is successful, you will get a message on the screen and a tick on the micro:bit.
Finally, press the reset button on the micro:bit to complete the pairing process.
How It Works
The micro:bit app allows you to create code, flash the compiled hex file onto micro:bit hardware, and interface with the device components (e.g., Camera) of a smartphone or tablet.
Connecting your micro:bit to your smartphone or tablet using Bluetooth for the first time is known as pairing. As a prerequisite, you must install an app on your Android or iOS device to pair, connect, and communicate with your micro:bit.
You can download the official micro:bit app for Android, developed by Samsung Electronics, UK, at Google play ( https://play.google.com/store/apps/details?id=com.samsung.microbit&hl=en ). This will require Android 4.4 or higher installed on your mobile device.
If you have an Apple iPhone or iPad, you can download the micro:bit app from iTunes app store at https://itunes.apple.com/us/app/micro-bit/id1092687276?mt=8 . The micro:bit app for iOS is currently compatible with a wide range of iPhone and iPad devices with different combinations of hardware components and iOS versions. The list of compatible devices can be found on the app’s download page.
13-3. Setting the Transmission Power
Problem
You want to set the transmission power of the Bluetooth module to 3.
Solution
In the Toolbox, click on the Bluetooth category. Then click and drag the bluetooth set transmit power block over and place it inside the on start block.
- Type 3 in the value box (Figure 13-5 ).
How It Works
The bluetooth set transmit power block allows you to set the transmission power of the Bluetooth radio module on your micro:bit board. You can provide the transmission power as a number in the range 0 to 7, where 0 is the lowest power and 7 is the highest power. The default power is 7.
Using high transmit power results in a longer range but requires more battery power.
13-4. Bluetooth Connecting
Problem
You want to display ‘connected’ or something like that on the screen when your phone (or tablet) gets connected to your micro:bit using Bluetooth.
Solution
In the Toolbox, click on the Bluetooth category. Then click on the on bluetooth connected event handler block.
- In the Toolbox, click on the Basic category. Next, click and drag the show string block over, and place it inside on bluetooth connected block. Then type Connected in the textbox (Figure 13-6 ).
How It Works
Any code you put inside the on bluetooth connected will run when something connects to your micro:bit using Bluetooth. This is very useful to indicate to users about the status of the Bluetooth connection between your smartphone (or tablet) and the micro:bit.
13-5. Bluetooth Disconnecting
Problem
You want to display ‘Disconnected’ or something like that on the screen when the Bluetooth connection gets disconnected between your phone (or tablet) and the micro:bit.
Solution
In the Toolbox, click on the Bluetooth category. Then click on the on bluetooth disconnected event handler block.
- In the Toolbox, click on the Basic category. Next, click and drag the show string block over, and place it inside on bluetooth disconnected block. Then type Disconnected in the textbox (Figure 13-7 ).
How It Works
Any code you put inside the on bluetooth disconnected will run when the Bluetooth connection disconnects between your phone and the micro:bit. This is very useful to indicate to users about the status of the Bluetooth connection between your smartphone (or tablet) and the micro:bit.
13-6. Using Bluetooth UART to Send String
Problem
You want to send text from your micro:bit to your Android running smartphone (or tablet) using the Bluetooth UART service.
Solution
- In the Toolbox, click on the Bluetooth category. Next, click and drag the bluetooth uart service block over, and place it inside the on start block (Figure 13-8 ).
In the Toolbox, click on the Input category and then click on the on button A pressed event block.
- In the Toolbox, click on the Bluetooth category. Next, click and drag the bluetooth uart write string block over, and place it inside the on button A pressed block. Then type Hello in the text box (Figure 13-9 ).
- Connect your micro:bit with the micro:bit UART terminal app by first clicking on the ‘double arrow’ button, followed by the selecting the micro:bit from the scanned device list (Figure 13-10 ).
How It Works
The Bluetooth UART (Universal Asynchronous Receiver/Transmitter) service allows you to exchange small chunks of data between your micro:bit and the smartphone (or tablet).