0

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?

Userrrrrrrrr
  • 183
  • 3
  • 12

1 Answers1

1

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

It's a stupid example.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @gedo I have no idea why Android would post such a stupid example, and neither do you, and SO is not the place for guesswork of this nature. I consider it far more likely that it was simply cut down from another example of something else and then the missing `break` added to make it work, but all this guesswork is completely off-topic. – user207421 Sep 21 '16 at 01:11