Bluez是一个开源的蓝牙协议栈,用于在Linux系统上实现蓝牙连接。"hcitool lescan"和"bluetoothctl scan on"都是用于扫描周围的蓝牙设备。但是,它们的工作方式略有不同。
使用"hcitool lescan"扫描蓝牙设备时,它只扫描LE(低功耗)蓝牙设备。而"bluetoothctl scan on"则会扫描所有类型的蓝牙设备,包括传统的BR/EDR(基本速率/增强数据率)设备。
以下是Bluez中使用"bluetoothctl scan on"的示例代码:
import time
from bluetooth.ble import DiscoveryService
# Start scanning for Bluetooth devices
print("Scanning for devices...")
service = DiscoveryService()
devices = service.discover(2)
# Print the name and addresses of any discovered device
print(f"Found {len(devices)} devices:")
for address, name in devices.items():
print(f" {name} ({address})")
请注意,在上面的代码示例中,我们使用"DiscoveryService"类来扫描设备,而不是使用"bluetoothctl"命令。这比使用命令行更方便和容易实现。