如何在 Windows 10 上为 Pybluez 解析 'PY_SSIZE_T_CLEAN macro must be defined for '#'格式'

How do I resolve 'PY_SSIZE_T_CLEAN macro must be defined for '#' formats' for Pybluez on Windows 10

我正在尝试 运行 Pybluez 服务器示例,但我收到一个我不明白的错误。有什么办法可以解决这个问题吗?我是 运行宁 Windows 10.

Traceback (most recent call last):
  File "C:\Users\cmbro\PycharmProjects\test\server.py", line 10, in <module>
    server_sock.bind(("", PORT_ANY))
  File "C:\Users\cmbro\PycharmProjects\test\venv\lib\site-packages\bluetooth\msbt.py", line 84, in bind
    bt.bind (self._sockfd, addr, port)
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

这是代码

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service( server_sock, "SampleServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
                   
print("Waiting for connection on RFCOMM channel %d" % port)

client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print("received [%s]" % data)
except IOError:
    pass

print("disconnected")

client_sock.close()
server_sock.close()
print("all done")

看起来您使用的是 Python 3.10? Python 3.10 中引入了重大更改(请参阅 bpo-40943 and PEP-0353). You need to update pybluez, see: https://github.com/pybluez/pybluez/issues/426