树莓派3 基于Python的pybluez库查询蓝牙

命令行:

sudo apt-get install python-dev

sudo apt-get install libbluetooth-dev

sudo pip3 install pybluez

注意:如果是Python3,必须pip3 ,前面一直用pip安装,一直不行,后面Google,pip3可以了,不知道Python2是否需要pip,未测试。

import bluetooth
print("looking for nearby devices...")
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))

def what_services(addr,name):
    print("%s-%s" % (addr,name))

    for services in bluetooth.find_service(address = addr):
        print("Name: %s" %(services["name"]))
        print("Description: %s" %(services["description"]))
        print("Protocol: %s" %(services["protocol"]))
        print("Provider: %s" %(services["provider"]))
        print("Port: %s" %(services["port"]))
        print("Service id: %s" %(services["service-id"]))
        print("")
        print("")

如果显示nearby_devices = bluetooth.discover_devices(lookup_names = True)错误,请完成源更新和系统软件更新,不再赘述,以及安装相应的蓝牙安装包等流程。具体链接蓝牙在我另一篇文章中。


你可能感兴趣的:(基于Python玩树莓派)