Linux BlueZ dbus通信a2dp [英] Linux BlueZ dbus communication a2dp
问题描述
作为一个简短的摘要,我目前正在构建Raspberry Pi,它具有充当Bluetooth A2DP接收器的功能,并将音频路由到3.5mm连接. 我运行的Python脚本之一使用BlueZ和Dbus与蓝牙设备进行通信,此刻,我正在使用的脚本找到了已与系统配对并自动连接到该设备的BT设备(脚本在cron上运行),然后,我可以在iPhone上按Play(播放)以开始音频流传输.但是,我要尝试的是找到设备并连接发送dBus命令以开始音频播放,而不必手动在电话上按Play本身.
As a quick summary I'm currently building a Raspberry Pi with the ability to act as a Bluetooth A2DP Receiver and routing that audio to a 3.5mm connection. One of the Python scripts I run uses BlueZ and Dbus to communicate with the bluetooth device, At the moment the script I'm using finds a BT device thats been paired with the system before and auto connects to it (Script runs on cron), I can then press Play on my iPhone to start the audio streaming.. However what I am trying to do is once it finds a device and connects to send a dBus command to start the audio playing instead of having to manually press play on the phone itself.
这里摘录了代码以及我为使代码正常运行而付出的一切.
Heres an extract of the code and what I've put in to attempt to make it work but with no luck.
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行.
In case its not easily seen I added in the line "devobj.Play()" about 4 lines from the bottom.
I get the error however:
iPhone具有A2DP音频源 org.freedesktop.DBus.Error.UnknownMethod:接口(空)"上签名为"的方法"Play"不存在
iPhone has A2DP audio source org.freedesktop.DBus.Error.UnknownMethod: Method "Play" with signature "" on interface "(null)" doesn't exist
推荐答案
如果您知道设备已经这样做:
If you know the device already do this:
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
device should be xx_xx_xx_xx_xx_xx
这篇关于Linux BlueZ dbus通信a2dp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!