Userrrrrrrrr
Userrrrrrrrr

Reputation: 183

In documentation, why is BluetoothServerSocket.accept() called in a while loop if it blocks the thread anyway?

In the android documentation, the following code occurs in the run() segment of a thread:

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;
            }
        }

However, the accept() method blocks the thread. I therefore do not understand why a while() loop is needed, especially since in all possible situations the while loop is broken in its first run.

Any ideas?

Upvotes: 0

Views: 123

Answers (1)

user207421
user207421

Reputation: 310883

Normally there wouldn't be a break after accepting and processing one socket: you would loop accepting sockets indefinitely.

It's a stupid example.

Upvotes: 1

Related Questions