蓝牙服务¶
蓝牙服务 (Bluetooth service) 专门用于与蓝牙设备进行交互,支持以下协议:
免提规范 (Hands-Free Profile, HFP):通过免提设备远程控制手机以及二者间的语音连接。
高级音频分发框架 (Advanced Audio Distribution Profile, A2DP):通过蓝牙连接播放多媒体音频。
音视频远程控制规范 (Audio Video Remote Control Profile, AVRCP):与 A2DP 同时使用,用于远程控制耳机、汽车音响系统或扬声器等设备。
应用示例¶
以下示例展示了该 API 的实现方式。
Header File¶
Functions¶
-
esp_err_t
bluetooth_service_start(bluetooth_service_cfg_t *config)¶ Initialize and start the Bluetooth service. This function can only be called for one time, and
bluetooth_service_destroymust be called after use.- Return
ESP_OK
ESP_FAIL
- Parameters
config: The configuration
-
audio_element_handle_t
bluetooth_service_create_stream()¶ Create Bluetooth stream, it is valid when Bluetooth service has started. The returned audio stream compatible with existing audio streams and can be used with the Audio Pipeline.
- Return
The Audio Element handle
-
esp_periph_handle_t
bluetooth_service_create_periph()¶ Create Bluetooth peripheral, it is valid when Bluetooth service has started. The returned bluetooth peripheral compatible with existing peripherals and can be used with the ESP Peripherals.
- Return
The Peripheral handle
-
esp_err_t
periph_bluetooth_play(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (PLAY) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_pause(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (PAUSE) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_stop(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (STOP) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_next(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (NEXT) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_prev(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (PREV) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_rewind(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (REWIND) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_fast_forward(esp_periph_handle_t periph)¶ Send the AVRC passthrough command (FAST FORWARD) to the Bluetooth device.
- Return
ESP_OK
ESP_FAIL
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_discover(esp_periph_handle_t periph)¶ Start device discovery.
- Return
ESP_OK : Succeed
ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
ESP_ERR_INVALID_ARG: if invalid parameters are provided
ESP_FAIL: others
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_cancel_discover(esp_periph_handle_t periph)¶ Cancel device discovery.
- Return
ESP_OK : Succeed
ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
ESP_FAIL: others
- Parameters
[in] periph: The periph
-
esp_err_t
periph_bluetooth_connect(esp_periph_handle_t periph, bluetooth_addr_t remote_bda)¶ Connect remote Device.
- Return
ESP_OK : Succeed
ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
ESP_FAIL: others
- Parameters
[in] periph: The periph[in] remote_bda: remote Bluetooth device address
-
esp_err_t
bluetooth_service_destroy()¶ Destroy and cleanup bluetooth service, this function must be called after destroying the Bluetoth Stream and Bluetooth Peripheral created by
bluetooth_service_create_streamandbluetooth_service_create_periph- Return
ESP_OK
ESP_FAIL
-
int
periph_bluetooth_get_a2dp_sample_rate()¶ Get a2dp sample rate.
- Return
sample rate
Structures¶
-
struct
bluetooth_service_user_cb_t¶ brief Bluetooth service user callback
-
struct
bluetooth_service_cfg_t¶ brief Bluetooth service configuration
Public Members
-
const char *
device_name¶ Bluetooth local device name
-
const char *
remote_name¶ Bluetooth remote device name
-
bluetooth_service_mode_t
mode¶ Bluetooth working mode
-
bluetooth_service_user_cb_t
user_callback¶ Bluetooth user callback
-
const char *
Macros¶
-
ESP_A2DP_SAMPLE_RATE¶
-
BLUETOOTH_ADDR_LEN¶ brief Bluetooth address length
Type Definitions¶
-
typedef uint8_t
bluetooth_addr_t[BLUETOOTH_ADDR_LEN]¶ brief Bluetooth device address