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

2024-05-20 20:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试列出所有附近的/可发现的蓝牙设备,包括那些已经配对的,它们在Linux上使用Python。

我知道如何使用地址列出设备的服务,并且可以成功连接:

services = bluetooth.find_service(address='...')

阅读PyBluez文档时,如果不指定任何条件,我希望附近的任何设备都会出现:

"If no criteria are specified, then returns a list of all nearby services detected."

我现在“唯一”需要的是能够列出已经配对的设备,不管它们是开的、关的、在附近还是不在。很像我在Ubuntu/Unity的所有设置中得到的列表-->;蓝牙。

顺便说一句,下面的并没有列出我的机器上已经配对的设备,即使它们在/附近。可能是因为配对后无法发现:

import bluetooth
for d in bluetooth.discover_devices(flush_cache=True):
    print d

有什么想法。。。?

编辑:我找到并安装了“bluez工具”。

bt-device --list

。。。提供我需要的信息,即添加设备的地址。

我已经检查了C源代码,发现这可能不像我想象的那么容易。

仍然不知道如何在Python中做到这一点。。。

编辑:我认为DBUS可能是我应该阅读的内容。似乎很复杂。如果有人有代码可以分享,我会很高兴的。:)


Tags: no文档编辑ifaddresslinux地址service
3条回答

我自己设法解决了这个问题。以下代码段列出了所有配对设备在我的默认蓝牙适配器上的地址:

import dbus

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object('org.bluez', '/'), 'org.bluez.Manager')

adapterPath = manager.DefaultAdapter()

adapter = dbus.Interface(bus.get_object('org.bluez', adapterPath), 'org.bluez.Adapter')

for devicePath in adapter.ListDevices():
    device = dbus.Interface(bus.get_object('org.bluez', devicePath),'org.bluez.Device')
    deviceProperties = device.GetProperties()
    print deviceProperties["Address"]

自从采用第5版蓝牙API以来,@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'
}]

参考: [1] http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/

您始终可以将其作为shell命令执行,并读取它返回的内容:

import subprocess as sp
p = sp.Popen(["bt-device", "--list"], stdin=sp.PIPE, stdout=sp.PIPE, close_fds=True)
(stdout, stdin) = (p.stdout, p.stdin)
data = stdout.readlines()

现在data将包含一个所有输出行的列表,您可以随意格式化和播放这些输出行。

相关问题 更多 >