Mic*_*cke 14 python linux bluetooth
我正在尝试在Linux上使用Python 列出所有附近/可发现的蓝牙设备,包括那些已配对的蓝牙设备.
我知道如何使用其地址列出设备的服务,并且可以成功连接:
services = bluetooth.find_service(address='...')
Run Code Online (Sandbox Code Playgroud)
阅读PyBluez文档如果我没有指定任何标准,我希望附近的任何设备都能显示出来:
"如果未指定任何条件,则返回检测到的所有附近服务的列表."
我现在需要的"唯一"事情是能够列出已经配对的设备,无论它们是开,关还是不在附近.就像我在Ubuntu/Unity中的All Settings - > Bluetooth中获得的列表一样.
顺便说一句,以下内容未列出我的机器上已配对的设备,即使它们在/附近.可能是因为配对后它们不可发现:
import bluetooth
for d in bluetooth.discover_devices(flush_cache=True):
print d
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ...?
编辑:我找到并安装了"bluez-tools".
bt-device --list
Run Code Online (Sandbox Code Playgroud)
...给我提供我需要的信息,即添加设备的地址.
我检查了C源,发现这可能不像我想象的那么容易.
仍然不知道如何在Python中这样做...
编辑:我认为DBUS可能是我应该阅读的内容.看起来很复杂.如果有人有一些代码要分享,我会非常高兴.:)
我设法自己解决了这个问题.以下代码段列出了我的默认蓝牙适配器上所有配对设备的地址:
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"]
Run Code Online (Sandbox Code Playgroud)
您始终可以将其作为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()
Run Code Online (Sandbox Code Playgroud)
现在data将包含所有输出行的列表,您可以根据需要进行格式化和播放.
自从采用蓝牙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"))
})
Run Code Online (Sandbox Code Playgroud)
在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'
}]
Run Code Online (Sandbox Code Playgroud)
参考:[1] http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/
| 归档时间: |
|
| 查看次数: |
16869 次 |
| 最近记录: |