简体   繁体   English

Linux BlueZ dbus通信a2dp

[英]Linux BlueZ dbus communication a2dp

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. 作为一个简短的摘要,我目前正在构建一个Raspberry Pi,它具有充当Bluetooth A2DP接收器的功能并将该音频路由到3.5mm连接。 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. 我运行的Python脚本之一使用BlueZ和Dbus与蓝牙设备进行通信,此刻,我正在使用的脚本找到了已与系统配对并自动连接到该设备的BT设备(脚本在cron上运行),然后,我可以在iPhone上按“播放”开始音频流。但是,我要尝试的是找到设备并连接发送dBus命令以开始音频播放,而不必手动在电话上按播放本身。

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)

In case its not easily seen I added in the line "devobj.Play()" about 4 lines from the bottom. 如果它不容易看到,我在底部的大约4行中添加“ devobj.Play()”行。

I get the error however: 但是我得到了错误:

iPhone has A2DP audio source org.freedesktop.DBus.Error.UnknownMethod: Method "Play" with signature "" on interface "(null)" doesn't exist iPhone具有A2DP音频源org.freedesktop.DBus.Error.UnknownMethod:接口“(null)”上具有签名“”的方法“ Play”不存在

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')

Then: 然后:

player.Play()

device should be xx_xx_xx_xx_xx_xx 设备应为xx_xx_xx_xx_xx_xx

I think it looks like the devobj is not constructed correctly when you call Play on it. 我认为当您调用Play时,似乎devobj的构造不正确。 I suggest you create the interface to the service in the same manner as you do in other parts of the code, ie calling dbus.Interface to obtain it. 我建议您以与代码其他部分相同的方式创建服务接口,即调用dbus.Interface来获取它。 Then you might have a better position to debug the part of the code which gives you errors. 然后,您可能会有更好的位置来调试部分代码,从而导致错误。

Also, investigating the bus you are working on could help you understand what it looks like and compare that to any assumptions you make in your code. 此外,研究正在使用的总线可以帮助您了解其外观,并将其与您在代码中所做的任何假设进行比较。 In a graphical environment you could use D-Feet, otherwise dbus-send (and in some cases dbus-monitor ) could be useful as well. 在图形环境中,可以使用D-Feet,否则dbus-send (在某些情况下为dbus-monitor )也可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM