蓝牙服务
蓝牙服务 (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_destroy
must be called after use.- 参数
config – The configuration
- 返回
ESP_OK
ESP_FAIL
-
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.
- 返回
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.
- 返回
The Peripheral handle
-
esp_err_t periph_bluetooth_play(esp_periph_handle_t periph)
Send the AVRC passthrough command (PLAY) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_pause(esp_periph_handle_t periph)
Send the AVRC passthrough command (PAUSE) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_stop(esp_periph_handle_t periph)
Send the AVRC passthrough command (STOP) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_next(esp_periph_handle_t periph)
Send the AVRC passthrough command (NEXT) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_prev(esp_periph_handle_t periph)
Send the AVRC passthrough command (PREV) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_rewind(esp_periph_handle_t periph)
Send the AVRC passthrough command (REWIND) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_fast_forward(esp_periph_handle_t periph)
Send the AVRC passthrough command (FAST FORWARD) to the Bluetooth device.
- 参数
periph – [in] The periph
- 返回
ESP_OK
ESP_FAIL
-
esp_err_t periph_bluetooth_discover(esp_periph_handle_t periph)
Start device discovery.
- 参数
periph – [in] The periph
- 返回
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
-
esp_err_t periph_bluetooth_cancel_discover(esp_periph_handle_t periph)
Cancel device discovery.
- 参数
periph – [in] The periph
- 返回
ESP_OK : Succeed
ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
ESP_FAIL: others
-
esp_err_t periph_bluetooth_connect(esp_periph_handle_t periph, bluetooth_addr_t remote_bda)
Connect remote Device.
- 参数
periph – [in] The periph
remote_bda – [in] remote Bluetooth device address
- 返回
ESP_OK : Succeed
ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
ESP_FAIL: others
-
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_stream
andbluetooth_service_create_periph
- 返回
ESP_OK
ESP_FAIL
-
int periph_bluetooth_get_a2dp_sample_rate()
Get a2dp sample rate.
- 返回
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 *device_name
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
Enumerations
-
enum bluetooth_service_mode_t
brief Bluetooth service working mode
Values:
-
enumerator BLUETOOTH_A2DP_SINK
A2DP Bluetooth sink audio, ESP32 will receive audio data from other bluetooth devices
-
enumerator BLUETOOTH_A2DP_SOURCE
A2DP Bluetooth source audio, ESP32 can send audio data to other bluetooth devices
-
enumerator BLUETOOTH_A2DP_SINK