🛠 Se connecter en BLE UART depuis un smartphone
Objectives
Expose a BLE "UART" service (serial profile) from the micro:bit V2 to send and receive text with a smartphone or PC, without a cable. Since the MicroPython radio module does not expose a serial GATT profile, we use MakeCode and its Bluetooth extension here.
Hardware
- 1 micro:bit V2 board
- 1 Android/iOS smartphone with a "micro:bit" or "Bluetooth Terminal" application
- Editor makecode.microbit.org (Bluetooth extension to be added)
Complete code
// MakeCode (JavaScript) — ajouter l'extension "Bluetooth"
// Profil Bluetooth: UART (réglage de l'extension)
let trame = ""
bluetooth.onBluetoothConnected(function {
basic.showString("BLE")
})
bluetooth.onUartDataReceived(serial.delimiters(Delimiters.NewLine),
function {
trame = bluetooth.uartReadUntil(serial.delimiters(Delimiters.NewLine))
if (trame === "ROUGE") {
basic.showString("R")
} else if (trame === "COEUR") {
basic.showIcon(IconNames.HEART)
} else {
basic.showString(trame)
}
})
basic.forever(function {
if (input.buttonIsPressed(Button.A)) {
bluetooth.uartWriteString("BOUTON A APPUYE\n")
basic.pause(500)
}
})
The micro:bit remains in BLE (SoftDevice S113): no Wi-Fi or Ethernet on this board. In MicroPython, the closest equivalent remains the radio module, which is not a standard GATT UART profile.
Expected verification
The application finds a "BBC micro:bit" device, connects, then: typing "HEART" + enter displays a heart on the matrix; pressing A sends "BUTTON A PRESSED" to the phone.
Common errors
- Bluetooth extension not added in MakeCode: the
bluetooth.*blocks do not exist. - Forgetting the newline delimiter in the terminal: the frame is never "received complete".
- Classic pairing fails: this BLE profile often does not use a PIN code, connect from the application side.
- The radio or BLE program is already occupied: only one connection at a time, disconnect the other app.
Other tutorials for this board
Code examples for this board
coeur.pyMicroPythonboutons.pyMicroPythonpodometre.pyMicroPythonlumiere.pyMicroPythonmusique.pyMicroPythonradio_ping.pyMicroPython