[ESP8266/ESP32]ESP32 作为蓝牙音源

9248浏览
查看: 9248|回复: 2

[ESP8266/ESP32] ESP32 作为蓝牙音源

[复制链接]
前面介绍过 ESP32 作为蓝牙音频接收端(蓝牙音箱),这里介绍它作为蓝牙音频的播放端。

首先需要确定蓝牙接收器的名称,用笔记本电脑连接后,可以再设备管理器中看到,这里我使用的是一款蓝牙耳机,名称是“JABRATALK”:

ESP32 作为蓝牙音源图1

接下来需要安装 ESP32-A2DP-master 这个库。下面的代码是从这个库的Example中修改而来,代码如下:

  1. /*
  2.   Streaming of sound data with Bluetooth to an other Bluetooth device.
  3.   We provide the complete sound data as a simple c array which
  4.   can be prepared e.g. in the following way
  5.   - Open any sound file in Audacity. Make sure that it contains 2 channels
  6.     - Select Tracks -> Resample and select 44100
  7.     - Export -> Export Audio -> Header Raw ; Signed 16 bit PCM
  8.   - Convert to c file e.g. with "xxd -i file_example_WAV_1MG.raw file_example_WAV_1MG.c"
  9.     - add the const qualifier to the array definition. E.g const unsigned char file_example_WAV_1MG_raw[] = {
  10.   Copyright (C) 2020 Phil Schatzmann
  11.   This program is free software: you can redistribute it and/or modify
  12.   it under the terms of the GNU General Public License as published by
  13.   the Free Software Foundation, either version 3 of the License, or
  14.   (at your option) any later version.
  15.   This program is distributed in the hope that it will be useful,
  16.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.   GNU General Public License for more details.
  19.   You should have received a copy of the GNU General Public License
  20.   along with this program.  If not, see <<a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/>.</a>
  21. */
  22. #include "BluetoothA2DPSource.h"
  23. #include "StarWars30.h"
  24. BluetoothA2DPSource a2dp_source;
  25. //SoundData *data = new TwoChannelSoundData((Channels*)StarWars10_raw,StarWars10_raw_len/4);
  26. SoundData *data = new OneChannelSoundData((int16_t*)StarWars30_raw, StarWars30_raw_len/2);
  27. void setup() {
  28.   Serial.begin(115200);
  29.   Serial.println("Start");
  30.   a2dp_source.start("JABRA TALK");   
  31. }
  32. void loop() {
  33.   if (a2dp_source.isConnected()==true) {
  34.       Serial.println("Connected!");
  35.       if (a2dp_source.hasSoundData()==true) {
  36.           Serial.println("Has sound!");
  37.         }
  38.       else {
  39.           Serial.println("No sound!");
  40.           a2dp_source.writeData(data);  
  41.       }
  42.     }
  43.     else {
  44.       Serial.println("Not connected!");
  45.     }
  46.   delay(2000);
  47. }
复制代码


特别注意,因为代码有音频数据需要特别选择 Huge APP 模式:
ESP32 作为蓝牙音源图2

测试使用的板子是ESP-WROOM-32,特点是价格偏移兼容性还不错:
ESP32 作为蓝牙音源图3




ESP32 作为蓝牙音源图4

测试的视频:



特别注意:连接时需要比较有耐心多次尝试,先将耳机设置为配对模式,然后ESP32上电。从资料来看,这样的搭配似乎有兼容性问题,淘宝上的卖家都不承诺蓝牙音频端能够兼容客户的蓝牙接收端。



gouba  学徒

发表于 2023-6-29 20:26:12

博主,有源码可以分享一下吗,谢谢
回复

使用道具 举报

zoologist  高级技匠
 楼主|

发表于 2023-6-30 15:30:05

gouba 发表于 2023-6-29 20:26
博主,有源码可以分享一下吗,谢谢

就是帖子最后的那个库
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail