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

小编典典

自从采用蓝牙API的第5版以来,@

Micke解决方案中使用的大多数功能都已删除,并且与总线的交互通过ObjectManager.GetManagedObjects [1]进行。

import dbus

def 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 result

bus = dbus.SystemBus()

# we need a dbus object manager

manager = 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 interface

devices = filter_by_interface(objects, "org.bluez.Device1")

# now we are ready to get the informations we need

bt_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'

}]

2020-06-07

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值