我正在尝试使用多个Raspi 3和RFCOMM创建一个蓝牙网络。因此,我编写了一个python脚本,它将运行一个服务器线程和两个客户端线程。客户端线程有时会尝试连接到同一设备,这会导致错误16:设备或资源繁忙。运行在一台设备上的两个客户端是否有可能同时连接到两个不同的服务。该应用程序对时间至关重要,这就是为什么我需要两个客户端并行运行的原因。我摘录了以下客户端线程的代码:
for service in service_available:
port = service["port"]
host = service["host"]
name = service["name"]
#address is the hex string form of the MAC Address without colons
address = host.replace(":","")
#The client goes back to discovery if the server already made the
#connection with the targeted device
with self.lock:
existingConnection = BT_Connection_Record.get(address)
if existingConnection is not None and existingConnection["Active"]:
continue
self.logger.info("<Bluetooth> Connecting to Device %s at %s port %s" % (name,host,port))
sock = BluetoothSocket(RFCOMM)
try:
sock.connect((host,port))
except IOError as e:
self.logger.warning("<Bluetooth> %s. Try the next available service"
% str(e))
continue
这偶尔会在输出中发生:
20:17:08-INFO:<Bluetooth Client 1> Connecting to Device X at (MAC address) port 1
20:17:08-INFO:<Bluetooth Client 2> Connecting to Device X at (MAC address) port 1
20:17:08-WARNING:<Bluetooth Client 2> (16, 'Device or resource busy'). Try the next available service
请注意,MAC地址是相同的,因此我知道客户端正在尝试在同一端口连接到完全相同的服务。这是我的问题吗?
是否可以进行两个connect()调用而不会出现错误16?感谢您的帮助!