树莓派连接蓝牙音箱

安装依赖包

sudo apt-get update 
sudo apt-get install pulseaudio pulseaudio-module-bluetooth
sudo apt-get install pi-bluetooth bluez bluez-firmware blueman
sudo apt install bluez bluez-firmware pulseaudio pulseaudio-module-bluetooth
sudo /etc/init.d/bluetooth restart

设置蓝牙配对连接音箱

bluetoothctl
show
power on
default-agent
scan on 
pair 41:42:90:F3:9E:B5
trust 41:42:90:F3:9E:B5
connect 41:42:90:F3:9E:B5
info 41:42:90:F3:9E:B5
exit

树莓派每次开机后都要执行此脚本来连接蓝牙音箱
/home/pi/myboot/startboot.sh

#!/bin/bash
sleep 1
pulseaudio --k
pulseaudio --start
bluetoothctl power on
bluetoothctl connect 41:42:90:F3:9E:B5
sleep 5
aplay  /home/pi/myboot/startaudio.wav
echo 'audio OK~~~'

*下面这个蓝牙开机自连脚本其实是有问题的,开机自启后看似蓝牙音箱是已连接状态了,但是播放音乐还是无声,还需要在当前SSH连接终端手动执行一次上面startboot.sh脚本才行。

开机启动用
/etc/init.d/startboot

#!/bin/sh
#/etc/init.d/startboot
### BEGIN INIT INFO
# Provides:startboot
# Required-Start:$remote_fs $syslog
# Required-Stop:$remote_fs $syslog
# Default-Start:2 3 4 5
# Default-Stop:0 1 6
# Short-Description: startboot
# Description: This service is used to start my applaction
### END INIT INFO

case "$1" in
     start)
     echo "start your app here."
     su pi -c "sh /home/pi/myboot/startboot.sh"
     ;;
     stop)
     echo "stop your app here."
     ;;
     *)
     echo "Usage: service startboot start|stop"
     exit 1
     ;;
esac
exit 0

启动服务
service startboot start

播放音频测试:

ffplay -nodisp -autoexit /home/pi/myboot/startaudio.wav 
aplay /home/pi/myboot/startaudio.wav 

猜你喜欢

转载自blog.csdn.net/ttyt1217/article/details/119688770