【问题标题】:How to open bluetooth connections and be A2DP source and sink如何打开蓝牙连接并成为 A2DP 源和接收器
【发布时间】:2017-11-23 02:32:32
【问题描述】:

我想使用 Python 连接蓝牙设备并将其用作扬声器。例如,使用 Python 播放 .wav 并在设备上收听并验证它正在播放等。我不想在我的 linux 机器上播放音频并在蓝牙设备上收听。

我正在查看stackoverflow,似乎我可以使用pybluez进行连接,但随后需要使用dbus来设置实际的音频连接。我找到了Linux BlueZ dbus communication a2dp,但结果是:

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

当它试图从界面中获取默认适配器时,我什至没有解决原始海报问题。感谢您的任何帮助! 代码如下:

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)

我有两个问题。

  1. 如何成为A2DP源?
  2. 如何解决获取适配器路径时抛出的异常?

【问题讨论】:

    标签: python linux audio bluetooth a2dp


    【解决方案1】:

    此代码不起作用的原因是 bluez 5.0 不再支持 org.bluez.Manager 类

    此处含糊描述:http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/

    bluez源码中有一些例子,即:test/list-devices 这里:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/list-devices

    这是该代码的 python3 版本:

    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("")
    

    【讨论】:

    • 谢谢!!这很棒。这也适用于 2.7(我们坚持),所以更好!真的很感激。
    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多