在 android 文档中,以下代码出现在线程的 run() 段中:
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
mmServerSocket.close();
break;
}
}
但是,accept() 方法会阻塞线程。因此,我不明白为什么需要 while() 循环,特别是因为在所有可能的情况下,while 循环在第一次运行时就被破坏了。
有任何想法吗?