© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_13

13. Using Bluetooth Services

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig1_HTML.jpg
    Figure 13-1.

    Extensions page

  • In the Some extensions will be removed window, click Remove extension(s) and add bluetooth button (Figure 13-2 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig2_HTML.jpg
    Figure 13-2.

    Confirmation dialog box

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

The following steps guide you on how to pair your micro:bit with a smartphone or tablet running on an Android operating system.
  • 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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig3_HTML.jpg
    Figure 13-3.

    The official micro:bit app

  • Choose INSTALL to install the app on your Android smartphone or tablet (Figure 13-4 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig4_HTML.jpg
    Figure 13-4.

    Installing the micro:bit app

  • 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.

The following steps guide you on how to pair your micro:bit with a smartphone or tablet running iOS.
  • 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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig5_HTML.jpg
    Figure 13-5.

    Full code listing

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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig6_HTML.jpg
    Figure 13-6.

    Full code listing

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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig7_HTML.jpg
    Figure 13-7.

    Full code listing

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

This solution assumes that you have already installed micro:bit UART Terminal app ( https://play.google.com/store/apps/details?id=com.ble.microbit.uart ) on your smartphone (or tablet) running Android and also paired your micro:bit with the same smartphone (or tablet) using Bluetooth.
  • 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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig8_HTML.jpg
    Figure 13-8.

    Placing the bluetooth uart services block

  • 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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig9_HTML.jpg
    Figure 13-9.

    Bluetooth UART writing

  • 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 ).
    ../images/474604_1_En_13_Chapter/474604_1_En_13_Fig10_HTML.jpg
    Figure 13-10.

    Connecting micro:bit with the UART terminal app

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).

The bluetooth uart write string block allows micro:bit to send data to a Bluetooth connected device. Sending text involves using the Bluetooth UART service so you must make sure that bluetooth uart service has been included in your code, usually inside the on start block. In the above solution under Recipe 13-6, when you press the button A, the string Hello will send to the Bluetooth connected smartphone (or tablet) over UART. The micro:bit UART terminal app will show the data chunks coming from the micro:bit (Figure 13-11 ).
../images/474604_1_En_13_Chapter/474604_1_En_13_Fig11_HTML.jpg
Figure 13-11.

Displaying data chunks on the UART terminal app

Similarly, you can use the bluetooth uart write number block to send numbers to a Bluetooth connected device. You can also use the bluetooth uart write value block to send values as a name-value pair to a Bluetooth connected device. This is useful when you want to send a set of two linked data items: a name, which is a unique identifier for some item of data; and the value, which is the data that is identified. As an example, the ambient temperature can be sent as shown in Figure 13-12 .
../images/474604_1_En_13_Chapter/474604_1_En_13_Fig12_HTML.jpg
Figure 13-12.

Sending the ambient temperature

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.215.96