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

查看: 160|回复: 0

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

[复制链接]
本帖最后由 zoologist 于 2020-10-12 15:07 编辑

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

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

bt1.png

接下来需要安装 ESP32-A2DP-master 这个库。下面的代码是从这个库的Example中修改而来,代码如下:
[C] 纯文本查看 复制代码
/*

  Streaming of sound data with Bluetooth to an other Bluetooth device.

  We provide the complete sound data as a simple c array which

  can be prepared e.g. in the following way

 

  - Open any sound file in Audacity. Make sure that it contains 2 channels

    - Select Tracks -> Resample and select 44100

    - Export -> Export Audio -> Header Raw ; Signed 16 bit PCM

  - Convert to c file e.g. with "xxd -i file_example_WAV_1MG.raw file_example_WAV_1MG.c"

    - add the const qualifier to the array definition. E.g const unsigned char file_example_WAV_1MG_raw[] = {

 

  Copyright (C) 2020 Phil Schatzmann

  This program is free software: you can redistribute it and/or modify

  it under the terms of the GNU General Public License as published by

  the Free Software Foundation, either version 3 of the License, or

  (at your option) any later version.

  This program is distributed in the hope that it will be useful,

  but WITHOUT ANY WARRANTY; without even the implied warranty of

  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License

  along with this program.  If not, see <[url=http://www.gnu.org/licenses/]http://www.gnu.org/licenses/>.[/url]

*/

 

#include "BluetoothA2DPSource.h"

#include "StarWars30.h"

BluetoothA2DPSource a2dp_source;

//SoundData *data = new TwoChannelSoundData((Channels*)StarWars10_raw,StarWars10_raw_len/4);

SoundData *data = new OneChannelSoundData((int16_t*)StarWars30_raw, StarWars30_raw_len/2);

 

void setup() {

  Serial.begin(115200);

  Serial.println("Start");

  a2dp_source.start("JABRA TALK");   

}

 

void loop() {

  if (a2dp_source.isConnected()==true) {

      Serial.println("Connected!");

      if (a2dp_source.hasSoundData()==true) {

          Serial.println("Has sound!");

        }

      else {

          Serial.println("No sound!");

          a2dp_source.writeData(data);  

      } 

    }

    else {

      Serial.println("Not connected!");

    }

  delay(2000); 

}

特别注意,因为代码有音频数据需要特别选择 Huge APP 模式:
bt2.png

测试使用的板子是ESP-WROOM-32,特点是价格偏移兼容性还不错:
bt3.png




bt4.png

测试的视频:



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

ESP32-A2DP-master.zip (1.95 MB, 下载次数: 8)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

硬件清单

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

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

mail