2024年11月24日星期日 下午10:05:06

树莓派通过Linux中蓝牙协议栈Bluz连接蓝牙音箱接入Home Assistnant

2 年 前
#3571 引用
树莓派通过Linux中蓝牙协议栈Bluz连接蓝牙音箱接入Home Assistnant

本文使用设备及版本:

树莓派3B+
Raspbian GNU/Linux 10 (buster)

BlueZ是Linux官方的蓝牙协议栈。Raspbian中已经安装了BlueZ。

Bluz是Linux中蓝牙协议栈的实现,可以用来分析蓝牙扫描和连接状况。可以通过以下命令查看Bluetooth的运行状态.
Bluez-该软件包提供蓝牙协议栈和bluetoothctl实用程序。

https://www.jianshu.com/p/2ea13615a69d




journalctl | grep blue


bluetoothd -v

systemctl status bluetooth


select B8:27:EB:2A:96:FA



sudo apt-get update && sudo apt-get -y dist-upgrade && sudo apt-get update

sudo apt-get remove bluez



[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select <ctrl>                                     Select default controller
devices                                           List available devices
paired-devices                                    List paired devices
system-alias <name>                               Set controller alias
reset-alias                                       Reset controller alias
power <on/off>                                    Set controller power
pairable <on/off>                                 Set controller pairable mode
discoverable <on/off>                             Set controller discoverable mode
discoverable-timeout [value]                      Set discoverable timeout
agent <on/off/capability>                         Enable/disable agent with given capability
default-agent                                     Set agent as the default one
advertise <on/off/type>                           Enable/disable advertising with given type
set-alias <alias>                                 Set device alias
scan <on/off>                                     Scan for devices
info [dev]                                        Device information
pair [dev]                                        Pair with device
cancel-pairing [dev]                              Cancel pairing with device
trust [dev]                                       Trust device
untrust [dev]                                     Untrust device
block [dev]                                       Block device
unblock [dev]                                     Unblock device
remove <dev>                                      Remove device
connect <dev>                                     Connect device
disconnect [dev]                                  Disconnect device
menu <name>                                       Select submenu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program
export  




Useful links

http://www.bluez.org/
https://github.com/bluez/bluez

编译安装bluez5.47蓝牙协议栈
https://blog.csdn.net/lee_jimmy/article/details/96739656

Bluez5介绍
https://blog.csdn.net/morixinguan/article/details/79197455

Bluetooth, AirPlay, and Spotify multi  room
https://pimylifeup.com/raspberry-pi-bluetooth-speakers/

ok
https://wiki.archlinux.org/title/Bluetooth#Auto_power-on_after_boot
0
2 年 前
#3572 引用




Step 1:首先要确保这两个服务已经启动


bluetooth 1749  1     3100   1628  800d0014 7ee111c4 S /system/bin/bluetoothd
ALSA



step 1:首先检查自己的BlueZ版本

bluetoothd -v

root@raspberrypi:~# bluetoothd -v
5.66



低版本的BlueZ对低功耗蓝牙的支持有限。如果你的使用版本低于5.43,那么我建议你升级BlueZ。




step 2:查看Bluetooth的运行状态

systemctl status bluetooth

这是bluez 4.3的输出

output

bluetooth.service - Bluetooth service
    Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2017-09-06 15:35:47 UTC; 2 months 28 days ago Docs: man:bluetoothd(8)
    Main PID: 557 (bluetoothd)
    Status: "Running"
    CGroup: /system.slice/bluetooth.service
            └─557 /usr/lib/bluetooth/bluetoothd
0
2 年 前
#3627 引用
在Raspbian中,基本的蓝牙操作可以通过bluez中的bluetoothctl命令进行。该命令运行后,将进入到一个新的Shell。在这个shell中输入:

bluetoothctl
0
2 年 前
#3628 引用
Way 1:使用基于BluZ的hciconfig/hcitool调试工具


树莓派 3 代 B+ 自带了 WIFI 和蓝牙功能,可以直接通过蓝牙连接音频设备。

使用 hciconfig 命令获取蓝牙模块的相关信息:



hciconfig

hciconfig -a

正常的:


$ hciconfig -a
hci0:   Type: Primary  Bus: UART
    BD Address: B8:27:EB:C3:38:31  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING
    RX bytes:4181 acl:136 sco:0 events:135 errors:0
    TX bytes:5065 acl:128 sco:0 commands:60 errors:0
    Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
    Link policy: RSWITCH SNIFF
    Link mode: SLAVE ACCEPT
    Name: 'raspberrypi'
    Class: 0x480000
    Service Classes: Capturing, Telephony
    Device Class: Miscellaneous,
    HCI Version: 4.2 (0x8)  Revision: 0xfc
    LMP Version: 4.2 (0x8)  Subversion: 0x6119
    Manufacturer: Broadcom Corporation (15)
/code]

本人的:

[code]
root@raspberrypi:~# hciconfig -a
hci0:  Type: Primary  Bus: UART
  BD Address: B8:27:EB:2A:96:FA  ACL MTU: 1021:8  SCO MTU: 64:1
  UP RUNNING PSCAN
  RX bytes:39708 acl:0 sco:0 events:2147 errors:0
  TX bytes:18310 acl:1 sco:0 commands:1150 errors:0
  Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
  Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
  Link policy: RSWITCH SNIFF
  Link mode: SLAVE ACCEPT
  Name: 'raspberrypi'
  Class: 0x580000
  Service Classes: Capturing, Object Transfer, Telephony
  Device Class: Miscellaneous,
  HCI Version: 4.1 (0x7)  Revision: 0x0
  LMP Version: 4.1 (0x7)  Subversion: 0x2209
  Manufacturer: Broadcom Corporation (15)



如果没有UP RUNNING PSCAN,则说明还没有说明hci0还未打开蓝牙适配器

HCI Version: 5.0就代表蓝牙5.0

树莓派连接移动机器人(蓝牙)
当然,如果你想用树莓派上的蓝牙也可以。在移动机器人上插上蓝牙模块。如果二者能配对成功并连接,那么万事大吉了。但是,我遇到的情况通常不能成功连上。解决的方法有很多,我这里只讲啥也不用安装、最快捷的一种。此时,移动机器人的蓝牙模块是 slave 模式的,需要改成 master 模式,让它主动连接。https://blog.csdn.net/qq_32617285/article/details/103186545



激活蓝牙(打开蓝牙适配器)

# hciconfig hci0 up

确认蓝牙激活成功


oot@raspberrypi:~# hcitool dev
Devices:
  hci0  B8:27:EB:2A:96:FA



8、蓝牙激活成功后,能够開始扫描周围的蓝牙设备

# hcitool scan

Scanning ...

90:C1:15:0F:C2:78 Xperia neo

//进行配对,说明hci0是设备id,0000是设置的密码,9C:4A:7B:C2:EA:81为"hcitool scan"扫描到的蓝牙设备地址
agent -a hci0 0000 9C:4A:7B:C2:EA:81
运行如下。
Pincode request for device /org/bluez/1749/hci0/dev_9C_4A_7B_C2_EA_81

ok 至此配对完成。

hcitool con 命令查看当前已连接的蓝牙设备

hcitool con


其他:

1.开放检測。同意周围设备检測到

# hciconfig hciX piscan

2.连接蓝牙

# hcitool cc A0:E9:DB:26:36:98

3. 配对

# hcitool auth A0:E9:DB:26:36:98


discoverable on
0
2 年 前
#3629 引用
Way 2:bluetoothctl

discoverable on


busctl tree org.bluez


root@raspberrypi:~# busctl tree org.bluez
└─/org
  └─/org/bluez
    └─/org/bluez/hci0
      ├─/org/bluez/hci0/dev_68_AB_BC_5D_A9_17
      ├─/org/bluez/hci0/dev_D0_70_C3_69_9E_96
      └─/org/bluez/hci0/dev_F6_E3_EB_CC_4D_70

0
2 年 前
#3630 引用
[SOLVED]bluetooth error - sap driver initialization failed

Get the bluetooth status:



root@raspberrypi:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-03-25 02:39:37 CST; 51min ago
     Docs: man:bluetoothd(8)
Main PID: 566 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/bluetooth.service
           └─566 /usr/lib/bluetooth/bluetoothd

3月 25 02:39:37 raspberrypi systemd[1]: Starting Bluetooth service...
3月 25 02:39:37 raspberrypi bluetoothd[566]: Bluetooth daemon 5.43
3月 25 02:39:37 raspberrypi systemd[1]: Started Bluetooth service.
3月 25 02:39:37 raspberrypi bluetoothd[566]: Starting SDP server
3月 25 02:39:37 raspberrypi bluetoothd[566]: Bluetooth management interface 1.14 initialized
3月 25 02:39:37 raspberrypi bluetoothd[566]: Failed to obtain handles for "Service Changed" characteristic
3月 25 02:39:37 raspberrypi bluetoothd[566]: Sap driver initialization failed.
3月 25 02:39:37 raspberrypi bluetoothd[566]: sap-server: Operation not permitted (1)
3月 25 02:39:37 raspberrypi bluetoothd[566]: Endpoint registered: sender=:1.10 path=/A2DP/SBC/Source/1
3月 25 02:39:37 raspberrypi bluetoothd[566]: Failed to set privacy: Rejected (0x0b)



Ok, solved by installing bluez4


apt  upgrade bluez


systemctl restart bluetooth
0
2 年 前
#3633 引用
[SOLVED]SAP stands for SIM Access Profile, so you have to disable it



Open /etc/systemd/system/bluetooth.target.wants/bluetooth.service
Change:

ExecStart=/usr/lib/bluetooth/bluetoothd
To

ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
Reload the systemd:

$ sudo systemctl daemon-reload
Restart the bluetooth:

$ sudo service bluetooth restart
Get the bluetooth status:

$ sudo service bluetooth status


bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Sat 2016-04-30 10:38:46 UTC; 6s ago
     Docs: man:bluetoothd(8)
Main PID: 12775 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─12775 /usr/lib/bluetooth/bluetoothd --noplugin=sap



https://stackoverflow.com/questions/53243651/raspberry-pi3-bluealsa-connected-but-no-sound-output-on-bluetooth-speaker
0
2 年 前
#3656 引用
[SOLVED]profiles/sap/server.c:sap_server_register() Sap driver initialization failed.



# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-03-26 00:40:55 CST; 7h ago
     Docs: man:bluetoothd(8)
Main PID: 13137 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/bluetooth.service
           └─13137 /usr/libexec/bluetooth/bluetoothd

3月 26 00:40:55 raspberrypi systemd[1]: Starting Bluetooth service...
3月 26 00:40:55 raspberrypi systemd[1]: Started Bluetooth service.
3月 26 00:40:55 raspberrypi bluetoothd[13137]: Bluetooth daemon 5.55
3月 26 00:40:55 raspberrypi bluetoothd[13137]: Starting SDP server
3月 26 00:40:55 raspberrypi bluetoothd[13137]: src/plugin.c:add_plugin() Version mismatch for sixaxis
3月 26 00:40:55 raspberrypi bluetoothd[13137]: Bluetooth management interface 1.14 initialized
3月 26 00:40:55 raspberrypi bluetoothd[13137]: profiles/sap/server.c:sap_server_register() Sap driver initia
3月 26 00:40:55 raspberrypi bluetoothd[13137]: sap-server: Operation not permitted (1)
3月 26 00:40:55 raspberrypi bluetoothd[13137]: Endpoint registered: sender=:1.36 path=/A2DP/SBC/Source/1

0
2 年 前
#3667 引用
[SOLVED]Failed to remove device: org.bluez.Error.NotReadbluetooth.service running, but bluetoothctl says “org.bluez.Error.NotReady”


remove EA:AE:C8:B0:5E:88


https://blog.csdn.net/LawssssCat/article/details/108458728




rfkill list
rfkill unblock all
0
2 年 前
#3668 引用
1
0