bluez python dbus,Linux BlueZ dbus通信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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值