Pybluez 找不到设备,但 bluetoothctl 可以

Dar*_*der 5 python bluetooth pybluez

我一直在尝试在运行 ubuntu 18 的 lenovo y50-70 笔记本电脑上运行一个简单的 python 蓝牙示例。但是在执行此操作时,我注意到 Pybluez 库无法找到任何设备,而使用 bluetoothctl 工具可以。

import bluetooth
print("performing inquiry...")
nearby_devices = bluetooth.discover_devices(duration=8, 
lookup_names=True, flush_cache=True, lookup_class=False)

print("found %d devices" % len(nearby_devices))

for addr, name in nearby_devices:
try:
    print("  %s - %s" % (addr, name))
except UnicodeEncodeError:
    print("  %s - %s" % (addr, name.encode('utf-8', 'replace')))
Run Code Online (Sandbox Code Playgroud)

运行上面的代码会得到输出:found 0 devices。运行 bluetoothctl 时给出以下输出:

[NEW] Controller 40:B8:9A:44:E0:46 daan-Lenovo-Y50-70 [default]
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller 40:B8:9A:44:E0:46 Discovering: yes
[NEW] Device 30:AE:A4:37:A2:4A 30-AE-A4-37-A2-4A
[NEW] Device D0:D2:B0:91:1C:C5 D0-D2-B0-91-1C-C5
[NEW] Device 70:73:CB:DF:1B:08 70-73-CB-DF-1B-08
[NEW] Device 51:80:89:6F:6E:B6 51-80-89-6F-6E-B6
[bluetooth]#
Run Code Online (Sandbox Code Playgroud)

我尝试以 root 身份运行我的 python 脚本,我当前的 pybluez 版本是 0.22。我希望有人知道如何解决这个问题。

小智 0

我的猜测是,您遇到了类似的问题如何设置 bluepy 可以在没有 sudo 的情况下扫描的环境?

在类似情况下对我有用的答案是:https: //stackoverflow.com/a/59798285/15546755