在 Linux 上用 Python 列出附近/可发现的蓝牙设备,包括已经配对 [英] List nearby/discoverable bluetooth devices, including already paired, in Python, on Linux
问题描述
我正在尝试在 Linux 上使用 Python 列出所有附近/可发现的蓝牙设备,包括那些已经配对的.
I'm trying to list all nearby/discoverable bluetooth devices, including those already paired, using Python on Linux.
我知道如何使用设备的地址列出设备的服务,并且可以成功连接:
I know how to list services for a device using its address, and can connect successfully:
services = bluetooth.find_service(address='...')
阅读 PyBluez 文档,如果我不指定任何条件,我希望附近的任何设备都能显示:
Reading the PyBluez docs I would expect any nearby device to show up if I don't specify any criteria:
如果未指定条件,则返回检测到的所有附近服务的列表."
"If no criteria are specified, then returns a list of all nearby services detected."
我现在唯一"需要的是能够列出已经配对的设备,无论它们是否打开、关闭、在附近.很像我在 Ubuntu/Unity 中的所有设置 --> 蓝牙中得到的列表.
The "only" thing I need right now is to be able to list already paired devices, whether they are on, off, nearby or not. Much like the list I'm getting in All Settings --> Bluetooth in Ubuntu/Unity.
顺便说一句,以下不会列出我机器上已经配对的设备,即使它们在/附近.可能是因为它们一旦配对就无法发现:
Btw, the following does not list already paired devices on my machine, even if they are on/nearby. Probably because they are not discoverable once paired:
import bluetooth
for d in bluetooth.discover_devices(flush_cache=True):
print d
有什么想法...?
我找到并安装了bluez-tools".
I found and installed "bluez-tools".
bt-device --list
... 提供我需要的信息,即添加设备的地址.
... gives me the information I need, i.e. addresses of added devices.
我检查了 C 源代码,发现这可能不像我想象的那么容易.
I've checked the C source, found out that this might not be as easy as I thought it would be.
仍然不知道如何在 Python 中执行此操作...
Still don't know how to do this in Python ...
我认为 DBUS 可能是我应该阅读的内容.看起来够复杂的.如果有人有一些代码可以分享,我会非常高兴.:)
I think DBUS might be what I should be reading up on. Seems complicated enough. If anyone has got some code to share I would be really happy. :)
推荐答案
自从蓝牙 API 第 5 版采用以来,@Micke 解决方案中使用的大部分功能都被删除了,并且交互与总线发生通过 ObjectManager.GetManagedObjects [1]
Since the adoption of the version 5 of the Bluetooth API most of the functions used in the @Micke solutions were dropped and the interaction with the bus take place throught the 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
列表中有包含所需数据的字典:即
In the bt_device
list there are dictionaries with the desired data:
ie
例如
[{
'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/
这篇关于在 Linux 上用 Python 列出附近/可发现的蓝牙设备,包括已经配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!