🛠 Se connecter en BLE UART depuis un smartphone

micro:bit V2 (BBC micro:bit v2) · intermediaire · Schéma : IMG-microbit-v2-intermediaire-3-microbit-v2-ble-uart.svg — smartphone affichant une application série Bluetooth à côté d'une micro:bit V2, flux de données BLE entre les deux et matrice LED affichant un message reçu

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials