清单附近/发现的蓝牙设备,包括已经配对,在Python中,在Linux上 [英] 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 /统一。
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
任何想法...?
Any ideas ...?
编辑:我找到并安装bluez的工具
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可能是什么,我应该在阅读起来。似乎很复杂。如果有人有一些code分享我会很高兴。 :)
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. :)
推荐答案
我设法解决自己的问题。下面的代码片段列出地址的所有配对设备,在我的默认蓝牙适配器:
I managed to solve the problem myself. The following snippet lists addresses for all paired devices, on my default bluetooth adapter:
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"]
这篇关于清单附近/发现的蓝牙设备,包括已经配对,在Python中,在Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!