简体   繁体   中英

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

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

Then:

player.Play()

device should be xx_xx_xx_xx_xx_xx

I think it looks like the devobj is not constructed correctly when you call Play on it. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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