gpt4 book ai didi

linux - (Bluez) 如何获取蓝牙耳机按键输入?

转载 作者:行者123 更新时间:2023-12-04 14:13:23 62 4
gpt4 key购买 nike

我有一个连接到 Linux 设备的蓝牙耳机。我想以编程方式接收播放/暂停/下一个/上一个/volUp/volDown 按钮按下。

经过一些研究,我发现我可以使用 dbus 与 Bluez 交互。我尝试使用此脚本注册 MediaPlayer https://github.com/bluez/bluez/blob/master/test/example-player但是当我检查 dbus 时,没有新的播放器,而且它似乎没有任何改变。

我也尝试过使用 Gatt 服务,但我在任何地方都找不到 org.bluez.GattService1。

获取输入的最佳方式是什么?我的 Bluez 有问题吗?

背景:我的目标是让音频输入和音频输出都通过蓝牙连接到 rpi。我确实设法做到了这一点,并在输出上播放了输入播放的声音。现在我也想转发输入。

最佳答案

我对此进行了一些调查并发现了一些东西,但没有得出完整的结论。我想我会在这里分享它,希望有人可以编辑/改进答案。

我已将蓝牙耳机连接到我的 Raspberry Pi,感兴趣的 DBus 接口(interface)似乎是 org.bluez.MediaControl1org.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 a{yv} - 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 a{qv} - 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 a{sv} - 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 a{sv} -
.Set method ssv - -
.PropertiesChanged signal sa{sv}as - -

(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 a{sv} -
.Set method ssv - -
.PropertiesChanged signal sa{sv}as - -
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 有一个 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' 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"

在我检查接口(interface)时,我一直在使用这段代码将其保持在 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 的问题。

关于linux - (Bluez) 如何获取蓝牙耳机按键输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62559473/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com