作为一个快速总结,我目前正在构建一个 Raspberry Pi,它能够充当蓝牙 A2DP 接收器并将音频路由到 3.5 毫米连接。我运行的其中一个 Python 脚本使用 BlueZ 和 Dbus 与蓝牙设备通信,目前我使用的脚本找到了一个之前与系统配对的 BT 设备并自动连接到它(脚本在 cron 上运行),然后我可以在我的 iPhone 上按 Play 开始音频流。但是我想做的是一旦它找到一个设备并连接发送一个 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:接口“(null)”上带有签名“”的方法“播放”不存在