需要安装pybluz
安装libbluetooth-dev依赖,避免直接安装出错
sudo apt-get install libbluetooth-dev
安装pybluez
sudo pip install pybluez
Example
官方例子inquiry.py
import bluetooth
print("performing inquiry...")
nearby_devices = bluetooth.discover_devices(lookup_names = True)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
print(" %s - %s" % (addr, name))
MIT Albert的bluetooth programming with python
findmyphone.py
import bluetooth
target_name = "My Phone"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
else:
print "could not find target bluetooth device nearby"