TB2026-YDS Firmware
Loading...
Searching...
No Matches
bluetooth.c File Reference

Classic Bluetooth A2DP source: stack bring-up, name-based discovery and connection. More...

#include "bluetooth.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "esp_pm.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "diag.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include <string.h>
#include <stdint.h>

Macros

#define BT_DEVICE_NAME   "TB2026-YDS"
#define BT_INQ_LEN   10 /* inquiry window: 10 * 1.28 s ~= 12.8 s */
#define BT_CONNECT_TIMEOUT_MS
#define BT_PAGE_TIMEOUT_SLOTS
#define BT_NVS_NS   "bt" /* NVS namespace for this service */
#define BT_NVS_KEY_KNOWN   "known" /* blob: the s_known array (count = blob size / entry) */
#define BT_AUTO_OFF_DELAY_MS
#define BT_PROF_EVT_TIMEOUT_MS

Functions

esp_err_t bluetooth_load_known (void)
 Load the persisted known-device list without powering the radio up.
esp_err_t bluetooth_init (void)
 Bring up the Bluetooth stack in A2DP source mode.
esp_err_t bluetooth_shutdown (void)
 Power the radio down: deinit A2DP/AVRCP, Bluedroid and the BR/EDR controller.
void bluetooth_set_auto_shutdown (bool enable)
 Enable/disable the automatic radio power-down when the A2DP link drops.
esp_err_t bluetooth_scan_start (void)
 Start an inquiry that collects nearby devices into a list (does not auto-connect).
esp_err_t bluetooth_scan_stop (void)
 Stop an inquiry started by bluetooth_scan_start(). No-op if not scanning.
bool bluetooth_is_scanning (void)
 Report whether an inquiry is currently running.
bool bluetooth_is_powered (void)
 Report whether the radio (controller + Bluedroid) is currently powered up.
size_t bluetooth_get_devices (bluetooth_device_t *out, size_t cap)
 Copy the current scan results into out (thread-safe snapshot).
esp_err_t bluetooth_connect (const esp_bd_addr_t bda)
 Connect the A2DP source to a device chosen by address (e.g. a scan result).
esp_err_t bluetooth_connect_by_name (const char *name_substr)
 TEST ONLY — scan and connect to the first device whose name contains name_substr.
esp_err_t bluetooth_disconnect (void)
 Disconnect the current A2DP link, if any.
bool bluetooth_is_connected (void)
 Report whether an A2DP source connection is currently established.
bluetooth_conn_state_t bluetooth_get_conn_state (void)
 Read the current connection state.
size_t bluetooth_get_known_devices (bt_known_device_t *out, size_t cap)
 Copy the persisted list of known (paired) devices into out, most-recent-first.
bool bluetooth_get_last_device (bt_known_device_t *out)
 Read the last successfully connected device (the one to offer for quick reconnect).
void bluetooth_set_last_device (const bt_known_device_t *dev)
 Remember a device as the most-recent one and persist it to NVS.
esp_err_t bluetooth_reconnect_last (void)
 Reconnect to the remembered device by address, without scanning.
esp_err_t bluetooth_forget_device (const esp_bd_addr_t bda)
 Forget a known device: drop it from the persisted list and remove its bond from NVS.
esp_err_t bluetooth_audio_start (esp_a2d_source_data_cb_t pcm_cb)
 Start streaming PCM to the connected sink through the A2DP source data path.
esp_err_t bluetooth_audio_stop (void)
 Suspend the media stream started by bluetooth_audio_start().
esp_err_t bluetooth_set_absolute_volume (uint8_t volume)
 Set the connected speaker's absolute volume over AVRCP (controller role).
bool bluetooth_is_avrc_connected (void)
 Report whether the AVRCP control channel is established.
bool bluetooth_volume_acked (void)
 Report whether the speaker has confirmed the most recent absolute-volume set.

Detailed Description

Classic Bluetooth A2DP source: stack bring-up, name-based discovery and connection.

Note
Développé avec l'assistance de Claude Opus 4.8 (Anthropic), sous la direction de Y. Dos Santos : spécification, revue et validation sur cible par l'auteur.

Macro Definition Documentation

◆ BT_AUTO_OFF_DELAY_MS

#define BT_AUTO_OFF_DELAY_MS
Value:
1000 /* link drop -> radio off: lets the stack finish the
teardown and player_poll (100 ms) unwind the BT sink */

◆ BT_CONNECT_TIMEOUT_MS

#define BT_CONNECT_TIMEOUT_MS
Value:
16000 /* abort a connect that never completes (radio stays on);
must exceed BT_PAGE_TIMEOUT_SLOTS so one full page
attempt can run to completion */

◆ BT_DEVICE_NAME

#define BT_DEVICE_NAME   "TB2026-YDS"

◆ BT_INQ_LEN

#define BT_INQ_LEN   10 /* inquiry window: 10 * 1.28 s ~= 12.8 s */

◆ BT_NVS_KEY_KNOWN

#define BT_NVS_KEY_KNOWN   "known" /* blob: the s_known array (count = blob size / entry) */

◆ BT_NVS_NS

#define BT_NVS_NS   "bt" /* NVS namespace for this service */

◆ BT_PAGE_TIMEOUT_SLOTS

#define BT_PAGE_TIMEOUT_SLOTS
Value:
24000 /* 15 s in 0.625 ms slots. After an abnormal drop the
speaker holds the dead link for its supervision
timeout (observed 24000 slots) and ignores paging
until then; the stock 5.12 s page timeout makes a
quick reconnect fail spuriously (HCI 0x04). */

◆ BT_PROF_EVT_TIMEOUT_MS

#define BT_PROF_EVT_TIMEOUT_MS
Value:
1000 /* wait for a PROF_STATE (profile init/deinit done) event;
normally arrives within a few ms (see prof_evt_wait) */