Raspberry Pi 无头蓝牙音频接收器

时间:2021-03-23 19:14:51

标签: bluetooth raspberry-pi

我有一个运行 Rasbian Buster 的 Raspberry Pi 4B。我正在用 Python 编写 Jukebox 软件,并且想要一个选项来暂停 Jukebox 操作并启用蓝牙音频接收器。再次按下该按钮,它会禁用蓝牙发现并重新启用 Jukebox 软件的正常处理。将树莓派安装到物理 Jukebox 后,它将无头运行(仍将使用带有 GUI 的 Rasbian O/S,但不连接监视器等)。

我的问题是我无法自动进行配对。我相信我的问题与 Blueman 有关,但不确定如何解决。当我配对我的 iPhone(或任何不受信任的设备)时,我在 Raspberry Pi 上看到一个对话框,指出:

已请求配对 设备“iPhone”已请求配对。你接受请求吗? 取消 确定

显然,我必须单击“确定”才能通过连接。这对我的原型来说很好,但需要弄清楚如何通过这个对话框(我是否卸载 Blueman,是否有我找不到的选项等)。

使用这些网站作为指南,这是我迄今为止所做的:

https://www.raspberrypi.org/forums/viewtopic.php?t=247892
https://sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html
https://www.raspberrypi.org/forums/viewtopic.php?t=235519
  1. sudo apt install bluealsa

  2. sudo nano /lib/systemd/system/bluealsa.service

  3. 编辑 ExecStart 行并在行尾添加“--profile=a2dp-sink”

  4. 完成更改后,按 Ctrl-X 保存并退出

  5. sudo adduser pi 蓝牙

  6. sudo nano /etc/systemd/system/aplay.service

     [Unit]
     Description=BlueALSA aplay service
     After=bluetooth.service
     Requires=bluetooth.service
    
     [Service]
     User=pi 
     ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
     Restart=on-failure  
     Restart=always
     RestartSec=30
    
     [Install]
     WantedBy=multi-user.target
    
  7. sudo systemctl enable aplay

  8. sudo nano /etc/rc.local

  9. 添加以下几行:

    /bin/hciconfig hci0 piscan &

    /bin/hciconfig hci0 sspmode 1 &

  10. sudo apt-get install bluez-tools

  11. sudo nano /etc/systemd/system/bt-agent.service

    [单位] 说明=蓝牙身份验证代理 之后=蓝牙.服务 PartOf=bluetooth.service

    【服务】 类型=简单 ExecStart=/usr/bin/bt-agent -c NoInputNoOutput ExecStartPost=/bin/sleep 1 ExecStartPost=/bin/hciconfig hci0 piscan ExecStartPost=/bin/hciconfig hci0 sspmode 1

    [安装] WantedBy=bluetooth.target

  12. sudo nano /etc/bluetooth/main.conf

  13. 取消注释/写出:

Class = 0x41C
DiscoverableTimeout = 0
  1. sudo nano /etc/machine-info

    PRETTY_HOSTNAME=点唱机

  2. 重启

如何跳过最后一个对话框?另外,当我需要蓝牙被发现时,是否有可以启动/停止的服务?

谢谢。布莱恩

1 个答案:

答案 0 :(得分:0)

有一篇关于在 Raspberry Pi 上使用蓝牙音频的好文章:

链接到一个很好的指南:

但是,它们都没有涵盖您的两个问题:如何打开/关闭发现,以及如何在不需要屏幕的情况下进行配对。

首先,您在几个地方使用了 hciconfig。该命令在 2017 年就已经是 deprecated。您应该考虑改用 bluetoothctl。但是,以下内容应该不需要这些设置步骤。

对于 Python,最好的方法是使用 BlueZ D-Bus API 来回答您的两个问题。 Adapter 接口上有一个 Discoverable 属性可以打开/关闭它。 还有一个 Agent API 可以让您接受连接而无需手动按下按钮。

有关如何使用这些 API 的示例,请访问:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test