如何在Python中查找可见的蓝牙设备?

9

我需要找到在我的蓝牙调制解调器范围内的可见蓝牙设备列表,以及它们各自的详细信息。我只需要处理蓝牙2.0及以下版本,不需要处理蓝牙4.0。

就像在Android手机上使用“搜索设备”一样。

很抱歉我没法提供我尝试过的代码,因为我不知道如何用Python处理蓝牙。


你想在哪个平台上进行这项操作?Android?Linux?还是其他什么平台? - Katie Kilian
@CharlieKilian 我希望能够在Linux和Windows上都做到这一点。 - Ufoguy
2个回答

18

PyBluez

from bluetooth import *

print "performing inquiry..."

nearby_devices = discover_devices(lookup_names = True)

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

for name, addr in nearby_devices:
     print " %s - %s" % (addr, name)

另请参阅使用Python编程蓝牙

重要的是可以使用lookup_names = True

来自bluez文档:

if lookup_names is False, returns a list of bluetooth addresses.
if lookup_names is True, returns a list of (address, name) tuples

预编译版本适用于Windows:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pybluez - jonincanada
PyBluez 看起来已经被弃用,在 Windows 上的 Python 3.10 中无法安装。 - Anderas

3
您可以使用PyBluez:
import bluetooth

nearby_devices = bluetooth.discover_devices()

你能发布一下它的输出示例吗? - Ufoguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接