是否有编程方法来查看被阻止的蓝牙设备?

Is there a programmatic method to view bluetooth devices that are blocked?

可以像这样阻止设备: bluetoothctl block FC:69:47:7C:9D:A3

是否有编程方法来列出已被阻止的设备?

是的,检查 info 文件:

grep Blocked /var/lib/bluetooth/*/FC:69:47:7C:9D:A3/info

会return:

Blocked=true

记录在案的与 BlueZ 蓝牙堆栈接口的方法是使用 D-Bus API。如果语言具有 D-Bus 绑定,则 D-Bus API 允许大多数语言与之交互。

这是一个使用 Python 和 pydbus 库的示例:

import pydbus

dev_iface = 'org.bluez.Device1'
bus = pydbus.SystemBus()
mngr = bus.get('org.bluez', '/')

mngd_objs = mngr.GetManagedObjects()

for path, info in mngd_objs.items():
    blocked = info.get(dev_iface, {}).get('Blocked')
    if blocked is not None:
        address = info.get(dev_iface, {}).get('Address')
        print(f'[{address}] is {"Blocked" if blocked else "Not Blocked"}')

BlueZ 设备 API 记录在:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt