我编写了一个代码,该代码使用我的笔记本电脑的蓝牙作为服务器来接受连接以及发送和接收数据。 但现在, 我已将程序转换为三个功能,其中一个功能 广告服务器,一个接受连接,另一个接收数据。 第一个函数工作正常,但是当我将server_socket地址传递给第二个函数时,它说:
Traceback (most recent call last):
File "/home/user/PycharmProjects/Stubx/BT_Server.py", line 170, in <module>
acceptconnections("29476601-52BE-11CB-8642-D50A896F8D2A", 2, 'bluetooth.bluez.BluetoothSocket object at 0x7f3e9a112a58')
File "/home/user/PycharmProjects/Stubx/BT_Server.py", line 52, in acceptconnections
client_sock, client_info = server_sock.accept()
AttributeError: 'str' object has no attribute 'accept'
<bluetooth.bluez.BluetoothSocket object at 0x7f3c80b28128>
如何将地址传递给第二个函数,以便它可以接受连接。
我已经使用return函数返回地址并将其传递给第二个函数,但是它显示了相同的错误
This is the Advertise Server Code:
from bluetooth import *
import time
def setupbtserver(uuid):
server_sock = BluetoothSocket(RFCOMM)
server_sock.bind(("", PORT_ANY))
server_sock.listen(1)
advertise_service(server_sock, "Sahil's Server",
service_id=uuid,
service_classes=[uuid, SERIAL_PORT_CLASS],
profiles=[SERIAL_PORT_PROFILE],
protocols=[RFCOMM_UUID]
)
print(server_sock)
return server_sock
接受连接的功能:
def acceptconnections(uuid, value, server_sock):
# Function to connect to the server
global client_sock
if value == 1:
try:
client_sock, client_info = server_sock.accept()
print("Accepted Connection From ", client_info)
except IOError:
print("Error")
这是断开或断开服务器的代码:
def teardownbtserver(server_sock):
server_sock.close()
This the error I am getting:
Traceback (most recent call last):
File "/home/user/PycharmProjects/Stubx/BT_Server.py", line 171, in <module>
acceptconnections("29476601-52BE-11CB-8642-D50A896F8D2A", 2, 'bluetooth.bluez.BluetoothSocket object at 0x7f3e9a112a58')
File "/home/user/PycharmProjects/Stubx/BT_Server.py", line 53, in acceptconnections
client_sock, client_info = server_sock.accept()
AttributeError: 'str' object has no attribute 'accept'
Process finished with exit code 1
预期输出: 接受的连接来自连接设备的MAC地址