0

I need to develop an application that will allow devices to communicate from time to time (one device sends a simple message, another one sends a response).

This communication is not going to happen too often (once in several minutes), so it does not seem to be reasonable to keep a permanent connection between the devices. Besides, the connection may be lost for some reason, so I'm going to need a some sort of the recovery logic.

Instead, I'd prefer the app to establish a new connection each time it needs to send data to a peer. However, the specs say, that after BluetoothServerSocket.accept returns, the server socket should be closed. This means that the app will no longer be able to receive the subsequent incoming connections.

Currently I'm thinking of not closing the server socket and processing the incoming connections in a synchronous manner, so that the app will not try to accept a new connection until the previous one is processed and closed. What will happen if another peer tries to connect in the middle of such processing? Will it fail or just block until server is ready to accept it?

4

1 回答 1

0

文档说“BluetoothServerSocket 可以(并且应该)被丢弃,除非你想接受更多的连接。” 既然你想接受更多的连接,那么你应该保留它。

文档还对 ServerSocket 的接受方法进行了以下说明。接受是一个阻塞调用。当连接被接受或发生异常时,它将返回。仅当远程设备发送的连接请求的 UUID 与在此侦听服务器套接字中注册的 UUID 匹配时,才接受连接。成功后,accept() 将返回一个已连接的 BluetoothSocket。

于 2013-06-21T13:52:57.393 回答