2024年11月24日星期日 上午7:20:21

bluetooth speaker is silent Checklist

2 年 前
#3693 引用
bluetooth speaker is silent Checklist

添加pi用户到蓝牙组
sudo usermod -G bluetooth -a pi,重启sudo reboot


HA使用树莓派连接蓝牙音箱原理

HA不能直接使用树莓派的蓝牙音箱,其内置的MPD播放器客户端,需要通过音频服务器来调用树莓派已连接ok的蓝牙音箱
https://bbs.hassbian.com/thread-5439-1-1.html


Bluz蓝牙技术的系统结构分为三大部分:底层硬件模块、中间协议层和应用层。

底层硬件模块:底层由蓝牙芯片实现

中间协议层:主机实现到HCI层。HCI层实现的是蓝牙芯片与主机通讯的方式






linux内Bluetooth的协议栈为BlueZ,http://www.bluez.org/

BlueZ 5.44体系结构

https://www.msly.cn/boards/topic/3206/1t/page/3#3721



一、 BlueZ 5.44体系结构

蓝牙技术的系统结构分为三大部分:底层硬件模块、中间协议层和应用层。

(1)底层硬件模块包括:无线跳频( RF)、基带( BB)和链路管理(LM)

  蓝牙的SIG规定了四种与硬件连接的物理总线方式USB、RS232、 UART和PC卡

(2)BlueZCore层包括各种Profile:

GAP:设备连接

SDAP:查询相关信息

SPP:虚拟串口连接

GOEP:数据传输

(3)中间协议层:

HCI接口:设备扫描、配对

L2CAP协议:蓝牙适配

(4)应用层包括BlueZ各种工具集:

hciconfig:配置蓝牙设备,用于打印设备信息和配置设备

hcitool:配置蓝牙连接,执行比如扫描等命令 -  已弃用

gatttool:一种直接使用参数对蓝牙设备进行控制,二就是使用-I参数进入interactive模式对蓝牙设备进行控制  - 已弃用

sdptool:搜索并管理设备服务




主机实现到HCI层,底层由蓝牙芯片实现。HCI层实现的是蓝牙芯片与主机通讯的方式。目前一般是串口或者USB通讯。所谓的USB也不是真正意义上的USB通讯,而是类似与USB转串口的方式,即通过驱动模拟USB设备实现串口通讯。目前USB蓝牙适配器基本都是这种设备模式。PC端实现了L2CAP, SDP, RFCOMM协议,以及USB转串口的驱动。Windows XP SP2操作系统以上版本的都内置了这些协议栈,还有如WIDCOMM等公司提供的第三方协议栈。

实际中只需在市场上购买这种蓝牙适配器(USB接口),然后通过配置内核蓝牙的接口驱动(即上图中的HCI层驱动),这样相应的蓝牙协议(linux官方版本是bluez)就已经在内核中了,这就相当于内核驱动中已经支持了相应的蓝牙协议(SDP,RFCOMM...),有了驱动就需要接口库提供给应用程序使用,这里用到的接口库是开源的bluez,其实就是要在内核之上移植bluez及工具bluez-utils。

https://www.cnblogs.com/embedded-linux/p/10507096.html

 Linux使用的蓝牙协议栈是Blue Z,不同于我们以往的开发方式,Blue Z提供的API 并不是通过头文件这样的形式, 而是通过D-Bus的方式来提供的。

  Blue Z提供的是Host端的协议栈,Control端的协议往往由专门的芯片厂提供。Host和Control通过HCI接口来通讯。HCI接口可以是UART、USB、SD卡,也可以是虚拟的。


                        
原文链接:https://blog.csdn.net/hesuping/article/details/131708132


Install the bluez package and enable the bluetoothd and dbus services.

Then, add your user to the bluetooth group and restart the dbus service, or simply reboot the system.

To use an audio device such as a wireless speaker or headset,
ALSA users need to install the bluez-alsa package. 
PulseAudio users do not need any additional software.
PipeWire users need libspa-bluetooth.

https://docs.voidlinux.org/config/bluetooth.html


安装
安装 bluez包,这个软件包提供蓝牙协议栈。
安装 bluez-utils包,这个软件包提供 bluetoothctl 实用程序。另外,也可以安装 bluez-utils-compatAUR 来获得#弃用的 BlueZ 工具。

蓝牙驱动
btusb 内核模块是通用蓝牙驱动。检查模块是否已加载。如果还没有,先加载模块。

启动/启用 bluetooth.service。
0
2 年 前
#3694 引用
对照这张图来检查


https://www.ipv6address.info/blog/bluez-544/

PulseAudio如何从BlueZ得到音频数据
虽然BlueZ内部对A2DP Sink的实现较为复杂,但是暴露给外部的数据接口确非常简单。在bluez/audio/ipc.c中实现了三个bt_audio_service函数。PulseAudio使用bt_audio_service_open()打开一个socket,然后调用bt_audio_service_get_data_fd()得到音频数据文件描述符fd。这个fd是通过那个socket从BlueZ的进程传递到PulseAudio的进程的。最后,使用完毕,调用bt_audio_service_close()来关闭socket。PulseAudio通过D-bus和BlueZ进行通信,进行参数的读取和设置,决定合适的读取时机,发送读取的状态。



PulseAudio从fd读出的音频数据流是经过SBC压缩编码的(对于采用其他编码,如MPEG-1,的情况,本文不做讨论),PulseAudio还需要对这些音频数据流进行解码。在BlueZ中已经实现了SBC编解码,源文件位于bluez/sbc。PluseAudio直接使用了这些源代码,把它们放在pulseadio/src/modules/bluetooth/sbc中。


step 1.Can you run bluetoothctl,If it doesn't, then there is a problem with bluetooth but at the OS level

Can you run bluetoothctl. It will drop you into a bluetooth shell. Can you then run 3 commands: power off, power on, scan on. And then wait.

If this works, you will see bluetooth events logged to the screen. You can try reloading the bluetooth integration and it might now work.
If it doesn't, then there is a problem with bluetooth but at the OS level.

https://github.com/home-assistant/core/issues/76544#top


OS level problem - Quick Start Checklist


#check rfkill state
#Make sure bluetooth is not blocked by rfkill
rfkill list

#see hci0 blocked, then you may just
rfkill unblock all

#check status of the bluetooth service
BlueZ是Linux官方的蓝牙协议栈。你可以通过BlueZ提供的接口,进行丰富的蓝牙操作
你可以用下面的命令检查BlueZ的运行状态
systemctl status bluetooth

#If it said "Dead" or "Inactive", try to restart it by
sudo systemctl restart bluetooth

#hciconfig
hciconfig -a

#If it said the hci0 is "DOWN", then try
hciconfig hci0 up




Before using the Bluetooth device, make sure that it is not blocked by rfkill.
By default, the Bluetooth adapter does not power on after a reboot-

ensure that your Bluetooth audio device is correctly paired and connected to the system


Unable to get hci controllers index list! Event frame status
There are no BT controllers present in the system!


Success Case:

Boot up the Pi.
Restart bluetooth service.
Start the agent, advertisement and gatt server.
Bond the device. Connect it.
Try to read the encrypted characteristic. It succeeds.
0
2 年 前
#3695 引用

1.由于Bluez的代码实现更新变化得很快,网上的许多文档介绍的都是bluz早期版本的使用;
2.文章多数是基于成熟的Linux发行版,基于内核内嵌的bluez 版本来讨论蓝牙设备的配置和使用;
3.对于嵌入系统开发,自己编译,搭建和配置相关环境的文章很少。


Installation

Install the bluez package, providing the Bluetooth protocol stack.
Install the bluez-utils package, providing the bluetoothctl utility. Alternatively install bluez-utils-compatAUR to additionally have the deprecated BlueZ tools.
The generic Bluetooth driver is the btusb kernel module. Check whether that module is loaded. If it is not, then load the module.
Start/enable bluetooth.service.

https://wiki.archlinux.org/title/Bluetooth#Auto_power-on_after_boot

Blueman是个非常方便的图形化蓝牙管理软件

安装好bluez之后,bluez默认会安装一些实用工具,比如hcitool,hciconfig ,gatttool





Step 1:Checking Bluetooth Status

Checking Bluetooth Status
Before you can add Bluetooth devices, the Bluetooth service on your computer must be up and running. You can check it with the help of the systemctl command.:

sudo systemctl status bluetooth


https://zhuanlan.zhihu.com/p/487809142



On your pi device, open System settings and make sure Bluetooth is turned on.

On the pi  first verify that Bluetooth is powered on.

Furthermore, make sure the to make the   discoverable.

蓝牙音频播放如果没有声音:
step 1:确认蓝牙模块是否被系统检测到  rfkill
首先第一步确认蓝牙连接已经建立。
如果使用PulseAudio,运行pactl list sinks来查看蓝牙设备有没有被识别,
运行pactl info来查看蓝牙设备是否是默认的音频输出设备。

https://niminghao.com/post/raspberry-pi-with-bluetooth-speaker/
0
2 年 前
#3696 引用
Can you run bluetoothctl. It will drop you into a bluetooth shell. Can you then run 3 commands: power off, power on, scan on. And then wait.

If this works, you will see bluetooth events logged to the screen. You can try reloading the bluetooth integration and it might now work.
If it doesn't, then there is a problem with bluetooth but at the OS level.
Logs of this would be helpful.
0
2 年 前
#3697 引用
Bluetooth Auto Recovery

https://github.com/Bluetooth-Devices/bluetooth-auto-recovery


1https://github.com/custom-components/ble_monitor/blob/master/custom_components/ble_monitor/bt_helpers.py

The code in this repo originates from https://github.com/custom-components/ble_monitor/blob/master/custom_components/ble_monitor/bt_helpers.py
0
2 年 前
#3698 引用
step 1.Bluetooth Adapter hardware Ready?


蓝牙设备不见了
https://www.msly.cn/boards/topic/10346/bluetooth-raspberry

use rfkill to find out whether your computer has detected the installed Bluetooth hardware of your computer.保未禁用蓝牙

failure case:

case 1:蓝牙模块未被系统检测到

oot@raspberrypi:~# rfkill
ID TYPE DEVICE    SOFT      HARD
0 wlan phy0   blocked unblocked


root@raspberrypi:~# hciconfig
root@raspberrypi:~# hciconfig -a
root@raspberrypi:~# hciconfig hci0
Can't get device info: No such device



Reason


# sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:bluetoothd(8)


case 2:蓝牙模块被禁用

root@raspberrypi:~# rfkill
ID TYPE      DEVICE    SOFT      HARD
0 wlan      phy0   blocked unblocked
1 bluetooth hci0   blocked unblocked



Success case:


:~# rfkill
ID TYPE      DEVICE      SOFT      HARD
0 wlan      phy0   unblocked unblocked
1 bluetooth hci0   unblocked unblocked



All the wireless hardware (i.e., Wi-Fi and Bluetooth) installed on your computer should be listed.

As you can see, a Bluetooth device is available on the list. It has the ID 0 on my computer.
0
2 年 前
#3699 引用
Bluetooth may be blocked on your computer



To use the Bluetooth hardware, you will have to unblock it as follows:


sudo rfkill unblock 0


NOTE: Here, 0 is the ID of the Bluetooth device.


root@raspberrypi:~# sudo rfkill unblock 0
root@raspberrypi:~# rfkill
ID TYPE      DEVICE      SOFT      HARD
0 wlan      phy0   unblocked unblocked
1 bluetooth hci0   unblocked unblocked



As you can see, the Bluetooth device is unblocked. Now, it’s ready to be used.


The persistent rfkill block/unblock flags are kept in /var/lib/systemd/rfkill.
You'll see several wlan files

Running rfkill on your Pi Zero will only alter one of those files and not touch the others.

/var/lib/systemd/rfkill
0
2 年 前
#3700 引用
Bluetooth service

The Bluetooth service should be active/running and enabled as marked in the screenshot below.
0
2 年 前
#3701 引用




bluetoothd -n -d

In order to debug, first stop bluetooth.service



root@raspberrypi:~# bluetoothd -n -d
bluetoothd[29680]: Bluetooth daemon 5.64
bluetoothd[29680]: src/main.c:check_config() Unknown group Controller in /etc/bluetooth/main.conf
bluetoothd[29680]: src/main.c:parse_config() parsing /etc/bluetooth/main.conf
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “DiscoverableTimeout” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “AlwaysPairable” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “PairableTimeout” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “Privacy” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “JustWorksRepairing” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “TemporaryTimeout” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “Name” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “Class” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “DeviceID” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “ReverseServiceDiscovery” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “RemoteNameRequestRetryDelay” in group “General”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “Cache” in group “GATT”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “KeySize” in group “GATT”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “ExchangeMTU” in group “GATT”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have key “Channels” in group “GATT”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have group “AVDTP”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have group “AVDTP”
bluetoothd[29680]: src/main.c:parse_config() Key file does not have group “AdvMon”
bluetoothd[29680]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[29680]: src/main.c:parse_mode_config() Key file does not have group “BR”
othd[29680]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[29680]: src/main.c:parse_mode_config() Key file does not have group “LE”

D-Bus setup failed: Name already in use
bluetoothd[29680]: src/main.c:main() Unable to get on D-Bus



0
2 年 前
#3702 引用
1
0