自从采用蓝牙API的第5版以来,@
Micke解决方案中使用的大多数功能都已删除,并且与总线的交互通过ObjectManager.GetManagedObjects [1]进行。
import dbusdef proxyobj(bus, path, interface): """ commodity to apply an interface to a proxy object """ obj = bus.get_object('org.bluez', path) return dbus.Interface(obj, interface)def filter_by_interface(objects, interface_name): """ filters the objects based on their support for the specified interface """ result = [] for path in objects.keys(): interfaces = objects[path] for interface in interfaces.keys(): if interface == interface_name: result.append(path) return resultbus = dbus.SystemBus()# we need a dbus object managermanager = proxyobj(bus, "/", "org.freedesktop.DBus.ObjectManager")objects = manager.GetManagedObjects()# once we get the objects we have to pick the bluetooth devices.# They support the org.bluez.Device1 interfacedevices = filter_by_interface(objects, "org.bluez.Device1")# now we are ready to get the informations we needbt_devices = []for device in devices: obj = proxyobj(bus, device, 'org.freedesktop.DBus.Properties') bt_devices.append({ "name": str(obj.Get("org.bluez.Device1", "Name")), "addr": str(obj.Get("org.bluez.Device1", "Address")) })在
bt_device列表中有与所需的数据字典:即
例如
[{ 'name': 'BBC micro:bit [zigiz]', 'addr': 'E0:7C:62:5A:B1:8C' }, { 'name': 'BBC micro:bit [putup]', 'addr': 'FC:CC:69:48:5B:32'}]参考:[1] http://www.bluez.org/bluez-5-api-introduction-and-porting-
guide/



