python蓝牙的使用 | 您所在的位置:网站首页 › 蓝牙ble模块 › python蓝牙的使用 |
环境
win10 python3.8 网上方式尝试首先网上大多数都是使用的pybluez这个库,但我安装时总出现问题,已经安装的有VS C++环境和win10SDK 我的方式 经历过上面的错误,开始查看这个库,发现GitHub上有另外一个工程pybluez2,并且更新的版本更高,于是尝试,安装成功 pip地址 https://pypi.org/project/pybluez2/ github地址 https://github.com/hiaselhans/pybluez2 使用方法可直接查看github的例子,非常好用 import bluetooth print(bluetooth.read_local_bdaddr()) server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) server_sock.bind(("", bluetooth.PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid, service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS], profiles=[bluetooth.SERIAL_PORT_PROFILE], # protocols=[bluetooth.OBEX_UUID] ) print("Waiting for connection on RFCOMM channel", port) client_sock, client_info = server_sock.accept() print("Accepted connection from", client_info) try: while True: data = client_sock.recv(1024) if not data: break print("Received", data) except OSError: pass print("Disconnected.") client_sock.close() server_sock.close() print("All done.") 其他发现python3.9版本 win已经支持蓝牙 https://docs.python.org/3.9/library/socket.html#module-socket 换成python3.9.4没有成功 |
CopyRight 2018-2019 实验室设备网 版权所有 |