gpt4 book ai didi

Linux BlueZ dbus 通信 a2dp

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:26 28 4
gpt4 key购买 nike

作为一个简短的总结,我目前正在构建一个 Raspberry Pi,它能够充当蓝牙 A2DP 接收器并将该音频路由到 3.5 毫米连接。我运行的 Python 脚本之一使用 BlueZ 和 Dbus 与蓝牙设备通信,目前我正在使用的脚本找到一个之前与系统配对的 BT 设备并自动连接到它(脚本在 cron 上运行),然后我可以在我的 iPhone 上按播放开始音频流。但是我想要做的是一旦它找到一个设备并连接发送一个 dBus 命令来开始播放音频而不是必须手动按手机上的播放本身。

这是代码的摘录,以及我为使其正常工作而添加的内容,但没有成功。

            bus = dbus.SystemBus()

#Get bluez dbus objects
man = bus.get_object('org.bluez', '/')
iface = dbus.Interface(man, 'org.bluez.Manager')
adapterPath = iface.DefaultAdapter()
adapter = dbus.Interface(bus.get_object('org.bluez', adapterPath),dbus_interface='org.bluez.Adapter')
devices = adapter.GetProperties()['Devices']

#for each device on this bluetooth adapter look for ones with A2DP sink service UUID and
# register for the propertychanged dbus signal
for d in devices:
dev = dbus.Interface(bus.get_object('org.bluez', d),dbus_interface='org.bluez.Device')
props = dev.GetProperties()
if any(AudioSourceServiceClass_UUID in UUID.upper() for UUID in props["UUIDs"]):
#This device is an A2DP Audio source
devobj = bus.get_object('org.bluez', d)
devobj.Trusted = True
if props["Connected"] == True:
print props["Name"] + " is connected!"
exit()

for d in devices:
dev = dbus.Interface(bus.get_object('org.bluez', d),dbus_interface='org.bluez.Device')
props = dev.GetProperties()
if any(AudioSourceServiceClass_UUID in UUID.upper() for UUID in props["UUIDs"]):
#This device is an A2DP Audio source
print props["Name"] + " has A2DP audio source"
#dev.connect_to_signal("PropertyChanged", handler_for_device(dev))
#dev.connect_to_signal("PropertyChanged", cb)
devobj = bus.get_object('org.bluez', d)
try:
devobj.Connect(dbus_interface='org.bluez.AudioSource')
devobj.Play()
exit()
except dbus.DBusException, e:
print str(e)

如果不容易看到,我在“devobj.Play()”行中添加了从底部算起大约 4 行。

但是我得到了错误:

iPhone 有 A2DP 音源org.freedesktop.DBus.Error.UnknownMethod:接口(interface)“(空)”上带有签名“”的方法“Play”不存在

最佳答案

如果您知道设备已经这样做了:

player = dbus.Interface(bus.get_object('org.bluez', '/org/bluez/hci0/dev_' + device.replace(":","_") + '/player0'), 'org.bluez.MediaPlayer1')

然后:

player.Play()

设备应该是xx_xx_xx_xx_xx_xx

关于Linux BlueZ dbus 通信 a2dp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329010/

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