gpt4 book ai didi

python - 如何打开蓝牙连接并成为A2DP源和接收器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:17 28 4
gpt4 key购买 nike

我想使用 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. 如何修复获取适配器路径时抛出的异常?

最佳答案

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

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

bluez源码中有一些例子,即:test/list-devices here: 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("")

关于python - 如何打开蓝牙连接并成为A2DP源和接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654891/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com