栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Linux上以Python列出附近/可发现的蓝牙设备,包括已配对的蓝牙设备

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Linux上以Python列出附近/可发现的蓝牙设备,包括已配对的蓝牙设备

自从采用蓝牙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/



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409000.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号