(Bluez) 如何获取蓝牙耳机按键输入?
Posted
技术标签:
【中文标题】(Bluez) 如何获取蓝牙耳机按键输入?【英文标题】:(Bluez) How to get bluetooth headset buttons input? 【发布时间】:2020-10-14 23:22:00 【问题描述】:我有一个连接到 Linux 设备的蓝牙耳机。 我想以编程方式接收播放/暂停/下一个/上一个/volUp/volDown 按钮按下。
经过一些研究,我发现我可以通过 dbus 与 Bluez 进行交互。 我尝试使用此脚本https://github.com/bluez/bluez/blob/master/test/example-player 注册 MediaPlayer 但是当我检查 dbus 时,没有新的播放器,而且它似乎没有改变任何东西。
我也尝试使用 Gatt 服务,但在任何地方都找不到 org.bluez.GattService1。
获取输入的最佳方式是什么?我的 Bluez 有问题吗?
背景: 我的目标是让音频输入和音频输出都通过蓝牙连接到 rpi。 我确实设法做到了这一点,并让输入播放的声音在输出上播放。 现在我也想转发输入。
【问题讨论】:
【参考方案1】:我对此进行了一些研究并发现了一些东西,但没有得出完整的结论。我想我会在这里分享它,希望有人可以编辑/改进答案。
我已将蓝牙耳机连接到我的 Raspberry Pi,感兴趣的 DBus 接口似乎是 org.bluez.MediaControl1
和 org.bluez.MediaTransport1
(venv) pi@raspberrypi:~/stack_overflow $ busctl tree 'org.bluez'
└─/org
└─/org/bluez
├─/org/bluez/hci0
│ ├─/org/bluez/hci0/dev_74_5C_4B_F8_6D_95
│ │ └─/org/bluez/hci0/dev_74_5C_4B_F8_6D_95/fd2
└─/org/bluez/test
(venv) pi@raspberrypi:~/stack_overflow $ busctl introspect 'org.bluez' '/org/bluez/hci0/dev_74_5C_4B_F8_6D_95'
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
org.bluez.Device1 interface - - -
.CancelPairing method - - -
.Connect method - - -
.ConnectProfile method s - -
.Disconnect method - - -
.DisconnectProfile method s - -
.Pair method - - -
.Adapter property o "/org/bluez/hci0" emits-change
.Address property s "74:5C:4B:F8:6D:95" emits-change
.AddressType property s "public" emits-change
.AdvertisingData property ayv - emits-change
.AdvertisingFlags property ay - emits-change
.Alias property s "Jabra Move SE v1.0.0" emits-change writable
.Appearance property q - emits-change
.Blocked property b false emits-change writable
.Class property u 2360324 emits-change
.Connected property b true emits-change
.Icon property s "audio-card" emits-change
.LegacyPairing property b false emits-change
.ManufacturerData property aqv - emits-change
.Modalias property s - emits-change
.Name property s "Jabra Move SE v1.0.0" emits-change
.Paired property b true emits-change
.RSSI property n - emits-change
.ServiceData property asv - emits-change
.ServicesResolved property b true emits-change
.Trusted property b false emits-change writable
.TxPower property n - emits-change
.UUIDs property as 5 "00001108-0000-1000-8000-00805f9b34fb… emits-change
org.bluez.MediaControl1 interface - - -
.FastForward method - - deprecated
.Next method - - deprecated
.Pause method - - deprecated
.Play method - - deprecated
.Previous method - - deprecated
.Rewind method - - deprecated
.Stop method - - deprecated
.VolumeDown method - - deprecated
.VolumeUp method - - deprecated
.Connected property b true emits-change
.Player property o - emits-change
org.freedesktop.DBus.Introspectable interface - - -
.Introspect method - s -
org.freedesktop.DBus.Properties interface - - -
.Get method ss v -
.GetAll method s asv -
.Set method ssv - -
.PropertiesChanged signal sasvas - -
(venv) pi@raspberrypi:~/stack_overflow $ busctl introspect 'org.bluez' '/org/bluez/hci0/dev_74_5C_4B_F8_6D_95/fd2'
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
org.bluez.MediaTransport1 interface - - -
.Acquire method - hqq -
.Release method - - -
.TryAcquire method - hqq -
.Codec property y 0 emits-change
.Configuration property ay 4 17 21 2 51 emits-change
.Delay property q - emits-change
.Device property o "/org/bluez/hci0/dev_74_5C_4B_F8_6D_95" emits-change
.State property s "idle" emits-change
.UUID property s "0000110A-0000-1000-8000-00805F9B34FB" emits-change
.Volume property q - emits-change writ
org.freedesktop.DBus.Introspectable interface - - -
.Introspect method - s -
org.freedesktop.DBus.Properties interface - - -
.Get method ss v -
.GetAll method s asv -
.Set method ssv - -
.PropertiesChanged signal sasvas - -
lines 1-19/19 (END)
org.bluez.MediaControl1
旁边已弃用,org.bluez.MediaTransport1
记录在:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/media-api.txt#n587
org.bluez.MediaTransport1
有一个音量属性,但是当我尝试读取它时出现错误:
(venv) pi@raspberrypi:~/stack_overflow $ busctl get-property 'org.bluez' '/org/bluez/hci0/dev_74_5C_4B_F8_6D_95/fd2' 'org.bluez.MediaTransport1' Volume
No such property 'Volume'
但我可以很好地阅读其他属性:
(venv) pi@raspberrypi:~/stack_overflow $ busctl get-property 'org.bluez' '/org/bluez/hci0/dev_74_5C_4B_F8_6D_95/fd2' 'org.bluez.MediaTransport1' State
s "active"
在我自省接口时,我一直在使用这段代码将其保持在Active
状态。
import os
from gi.repository import GLib
from pydbus import SystemBus
from time import sleep
dev_addr = '74:5C:4B:F8:6D:95'
bluez_service = 'org.bluez'
adapter_path = '/org/bluez/hci0'
device_path = f"adapter_path/dev_dev_addr.replace(':', '_')"
bus = SystemBus()
mngr = bus.get(bluez_service, '/')
adapter = bus.get(bluez_service, adapter_path)
def get_media_transport_path(dev_path):
mng_objs = mngr.GetManagedObjects()
for dev_objs in mng_objs:
if dev_objs.startswith(dev_path):
state = mng_objs[dev_objs].get('org.bluez.MediaTransport1', ).get('State', False)
print('State', state)
if state:
return dev_objs
mt_path = get_media_transport_path(device_path)
mt = bus.get(bluez_service, mt_path)
mt_data = mt.Acquire()
print('Acquire', mt_data)
print('GetAll', mt.GetAll('org.bluez.MediaTransport1'))
print('Transport Device', mt.Device)
print('Transport state', mt.State)
sleep(20)
mt.Release()
print('Transport State', mt.State)
我的下一个调查将围绕 Acquire
返回的文件描述符,但是我选择的 DBus 库在 fd 的 https://github.com/LEW21/pydbus/pull/86 周围存在问题
【讨论】:
我也面临同样的问题,我无法获得 Volume 属性,我希望它可以在我的 Sony wf-1000xm3 上提供当前的“硬件”音量级别。 :(到目前为止你有什么进展吗?【参考方案2】:我迟到了,但 Arch Linux Wiki 对此提供了很好的说明:https://wiki.archlinux.org/index.php/Bluetooth_headset#Configuration_via_CLI
安装bluez-utils
并运行mpris-proxy
。这会将命令转发到 mpris2 界面,您的媒体播放器可以在其中拾取它。
【讨论】:
以上是关于(Bluez) 如何获取蓝牙耳机按键输入?的主要内容,如果未能解决你的问题,请参考以下文章
蓝牙:如何使用 Bluez 创建一个套接字以连接到具有特定 UUID 的服务?
如何使用 dbus/bluez 在 python 中获取当前 MTU
Bluez(蓝牙)在连接时没有发送 DBUS method_call SelectConfiguration 吗?如果是这样,我该如何抓住它?