01
01
Project Introduction
It has been a while since I updated the content because I was developing a mini program for a controller that communicates with the micro:bit development board via Bluetooth, which can be used for remote control of various small vehicles.
The previous courses have introduced the Bluetooth module of the micro:bit, which can be controlled after installing the mobile app. During my usage, I found many aspects not user-friendly, so I developed the “Micro Blue Controller” WeChat mini program to address these issues:
-
Discovering that the micro:bit Bluetooth operation is too deep, it is necessary to remove the option to display unpaired micro:bits on more interfaces; -
Re-pairing after the micro:bit is powered off is complicated and difficult to connect; -
The original app has other functions that are not practical besides controlling the controller; -
There are too many program events for controlling the controller, making programming not user-friendly; -
The button-based directional control cannot be used for smooth turns;
02
02
Principle Analysis
2.1 Communication Protocol
The Micro Blue Controller mini program exchanges data with the micro:bit board: each command is separated by #
-
The commands for Button A, Button B, Button C, and Button D are: A#, B#, C#, D# -
The joystick is centered at the origin, with right as the X-axis and up as the Y-axis. When the joystick is moved, it sends both the x-axis and y-axis coordinates to the micro:bit: 0,100#
2.2 Bluetooth
The mini program and the micro:bit board use Low Energy Bluetooth (Bluetooth Low Energy, BLE): a protocol supported since Bluetooth 4.0, characterized by low power consumption and faster transmission speed.
Let’s first understand a few technical terms:
- Device ID (deviceId): Each Bluetooth peripheral device has a unique
<span><span><span>deviceId</span></span></span>
for identification. - Service: A service is provided by the Bluetooth device to the outside. A device can offer multiple services, such as battery information service, system information service, etc. Each service is uniquely identified by a UUID.The Bluetooth serial service for Microbit is fixed at: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E.
- Characteristic: Each Service contains 0 to multiple Characteristics. For example, the battery information service will have a Characteristic representing the battery data. A Characteristic consists of a value and 0 to multiple Descriptors. Communication with Bluetooth devices mainly involves reading and writing the value of the Characteristic. Each Characteristic is uniquely identified by a UUID.
The Bluetooth usage process
- 1. Initialize the Bluetooth module:
wx.openBluetoothAdapter // Initialize the Bluetooth adapter module
- 2. Scan and discover Bluetooth peripheral devices:
wx.onBluetoothDeviceFound // Listen for new device found eventwx.startBluetoothDevicesDiscovery // Start searching for nearby Bluetooth peripheral devices
- 3. Connect to the device:
wx.createBLEConnection // Establish a Bluetooth Low Energy connection
- 4. Get services of the Bluetooth peripheral device:
wx.getBLEDeviceServices // Get Bluetooth Low Energy device services
- 5. Get the characteristic value of the write service:
wx.getBLEDeviceCharacteristics // Get Bluetooth Low Energy device characteristic values
- 6. Write characteristic value data:
wx.writeBLECharacteristicValue // Write Bluetooth Low Energy characteristic value
03
03
Program Implementation
3.1 Makecode Program
First, install the “bluetooth” extension, and after installation, add the “Bluetooth Serial Service” and “Serial Redirect to USB” in the “When powered on” program block. When the Bluetooth connection is successful, a checkmark is displayed; when disconnected, a cross is shown. When receiving data separated by #, write that data to the serial port.

3.2 Set Bluetooth Pairing Mode, Download Program, and Enable Bluetooth Pairing
In the “Project Settings”, set Bluetooth to not require pairing, then download the program to the Microbit via USB cable. While holding down the microbit’s Button A and Button B, press the reset button on the back of the microbit, then release after 2 seconds. Release Button A and Button B to enter Bluetooth pairing mode.


3.3 Micro Blue Controller Mini Program Connects Bluetooth
Follow the “Micro Blue Classroom” public account, send a message, and enter the “Micro Blue Controller” mini program from the bottom menu. Click on the Bluetooth name of the Microbit in the device list, and after establishing a connection, it will automatically enter the controller interface.



04
04
Effect Display
Return to the makecode editor and click “Show data device”, then click any button or slide the joystick to play, and you can see the real-time display of our operation commands in the serial port.
