作为一个简单的总结,我目前正在构建一个树莓pi,它能够充当蓝牙a2dp接收器,并将音频路由到3.5m m的连接。
我运行的其中一个python脚本使用bluez和dbus与蓝牙设备通信,此时我正在使用的脚本发现一个bt设备以前与系统配对并自动连接到它(脚本在cron上运行),然后我可以按iphone上的play启动音频流。不过,我想做的是一旦它找到一个设备,并连接发送一个dbus命令开始音频播放,而不是必须手动按播放手机本身。
这是代码的一个摘录,以及我所做的努力,以使它工作,但没有运气。

            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行。
但是我得到了错误:

iPhone有A2DP音频源
OrgDeffToop.dBo.Error .UnNo.No.Mead:方法“玩”签名“接口”(NULL)不存在

最佳答案

如果您知道设备已执行此操作:

player = dbus.Interface(bus.get_object('org.bluez', '/org/bluez/hci0/dev_' + device.replace(":","_") + '/player0'), 'org.bluez.MediaPlayer1')

然后:
player.Play()

设备应为xx嫒xx嫒xx嫒xx嫒

关于linux - Linux BlueZ dbus通信a2dp,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33329010/

10-12 16:22