🛠 Bluetooth LE : serveur GATT avec la radio 5.2

Raspberry Pi Pico 2 W · intermediaire · Schéma : IMG-pico-2-w-intermediaire-6-pico2w-ble-serveur-gatt.svg — Pico 2 W diffusant une annonce BLE vers un smartphone, service GATT personnalisé et caractéristique lecture/écriture.

Objectives

The Pico 2 W embeds the same CYW43439 as the Pico W: Bluetooth 5.2 confirmed, with BLE central and peripheral roles and even Bluetooth Classic. This tutorial sets up a GATT server: the board broadcasts an advertisement, a smartphone connects to it and reads a characteristic that counts seconds. BLE coexists with Wi-Fi on the same chip, but you must choose the btstack architecture instead of lwIP at compile time.

Hardware

Complete code

// main.c - squelette inspire de pico-examples pico_w/btstack
// lien : pico_btstack_cyw43 pico_stdlib
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "btstack.h"

static const uint8_t adv_data[] = {
    0x02, 0x01, 0x06,                       // flags LE general
    0x06, 0x09, 'P', 'i', 'c', 'o', '2'     // nom d'annonce
};

int main(void) {
    stdio_init_all;
    if (cyw43_arch_init) { return 1; }    // meme puce que le Wi-Fi
    cyw43_arch_enable_bt_mode;            // mode Bluetooth du CYW43
    // 1) hci_setup + btstack_run_loop
    // 2) att_server : service 16 bits 0xA0FB + caracteristique compteur (READ)
    // 3) gap_advertise(adv_data) ; dans la boucle, compteur++ chaque seconde
    while (true) { sleep_ms(1000); }
}

Expected verification

The smartphone's BLE scanner sees the Pico2 advertisement, connects, and the characteristic increases by one unit per second. The radio LED may blink during advertising.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials