Search code examples
pythonlinuxaudiobluetootha2dp

How to open bluetooth connections and be A2DP source and sink


I want to use Python to connect up to a bluetooth device and utilize that as a speaker. For example, use Python to play .wav and hear on device and validate it is being played etc. I than want to play audio on my linux box and listen to it on the bluetooth device.

I was doing some looking on stackoverflow and it seems I can use pybluez to connect, but then need to use dbus to set up the actual audio connection. I found Linux BlueZ dbus communication a2dp, but it results in:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "DefaultAdapter" with signature "" on interface "org.bluez.Manager" doesn't exist

When it tries to get the default adapter off the interface, so I do not even get to the original posters issue. Thank you for any help! Here is the code:

import dbus as dbus
bus = dbus.SystemBus()

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 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"]):
        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)

I have two questions.

  1. How to be a A2DP source?
  2. How do I fix the exception thrown when getting the adapter path?

Solution

  • The reason this code is not working is that the org.bluez.Manager class is no longer supported in bluez 5.0

    Vaguely described here: http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/

    There are some examples in the bluez source code, ie: test/list-devices here: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/list-devices

    Here is a python3 version of that code:

    from __future__ import absolute_import, print_function, unicode_literals
    
    import dbus
    
    bus = dbus.SystemBus()
    
    manager = dbus.Interface(bus.get_object("org.bluez", "/"),
                        "org.freedesktop.DBus.ObjectManager")
    
    def extract_objects(object_list):
        list = ""
        for object in object_list:
            val = str(object)
            list = list + val[val.rfind("/") + 1:] + " "
        return list
    
    def extract_uuids(uuid_list):
        list = ""
        for uuid in uuid_list:
            if (uuid.endswith("-0000-1000-8000-00805f9b34fb")):
                if (uuid.startswith("0000")):
                    val = "0x" + uuid[4:8]
                else:
                    val = "0x" + uuid[0:8]
            else:
                val = str(uuid)
            list = list + val + " "
        return list
    
    objects = manager.GetManagedObjects()
    
    
    all_devices = (str(path) for path, interfaces in objects.items() if
                        "org.bluez.Device1" in interfaces.keys())
    
    for path, interfaces in objects.items():
        if "org.bluez.Adapter1" not in interfaces.keys():
            continue
    
        print("[ " + path + " ]")
    
        properties = interfaces["org.bluez.Adapter1"]
        for key in properties.keys():
            value = properties[key]
            if (key == "UUIDs"):
                list = extract_uuids(value)
                print("    %s = %s" % (key, list))
            else:
                print("    %s = %s" % (key, value))
    
        device_list = [d for d in all_devices if d.startswith(path + "/")]
    
        for dev_path in device_list:
            print("    [ " + dev_path + " ]")
    
            dev = objects[dev_path]
            properties = dev["org.bluez.Device1"]
    
            for key in properties.keys():
                value = properties[key]
                if (key == "UUIDs"):
                    list = extract_uuids(value)
                    print("        %s = %s" % (key, list))
                elif (key == "Class"):
                    print("        %s = 0x%06x" % (key, value))
                elif (key == "Vendor"):
                    print("        %s = 0x%04x" % (key, value))
                elif (key == "Product"):
                    print("        %s = 0x%04x" % (key, value))
                elif (key == "Version"):
                    print("        %s = 0x%04x" % (key, value))
                else:
                    print("        %s = %s" % (key, value))
    
        print("")