我想使用python3(pyBluez0.23模块)进行蓝牙通信
我在互联网上找到了一些使用 Pybluez 库的教程,但这对我不起作用。 在下面找到服务器部分的代码:
import bluetooth
def setupServer():
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
print("Socket Created.")
port = 0
server_socket.bind("",port)
server_socket.listen(1)
print("Socket bind complete")
uuid = "xxx-xxx-xxxx-xxxx"
bluetooth.advertise_service(server_sock, "Wifi config",
service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE])
client_sock, client_info = server_sock.accept()
self.handle_client(client_sock)
client_sock.close()
server_sock.close()
这是输出:
<bluetooth.msbt.BluetoothSocket object at 0x000002736C4BB610>
bluetooth socket created.
Traceback (most recent call last):
File "ROS.py", line 99, in <module>
main()
File "ROS.py", line 96, in main
BluetoothServerNode().start()
File "ROS.py", line 67, in start
server_sock.bind(("", blt.PORT_ANY))
File "c:\opt\ros\noetic\x64\lib\site-packages\bluetooth\msbt.py", line 84, in bind
bt.bind (self._sockfd, addr, port)
OSError: A
根据https://github.com/pybluez/pybluez/issues/318,
OSError: A
很可能是错误信息显示中的一个bug,随后修复了这个问题,但0.23之后还没有另一个正式版本,所以您可以回滚到 0.22 或从最新的 master 安装(推荐):
pip install https://github.com/pybluez/pybluez.git#egg=pybluez