IOException w akceptuj wątek

Jedna część mojej aplikacji łączy się z urządzeniem przez Bluetooth i zwykle działa dobrze, ale czasami nie łączy się i pojawia się następujący błąd

03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed
03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:316)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:105)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:91)
03-11 10:29:20.328: E/BluetoothComService(8059):    at com.mypackage.name.bluetooth.BluetoothService$AcceptThread.run(BluetoothService.java:298)

To jest linia, w której otrzymuję wyjątek

socket = mmServerSocket.accept();    

I to jest kompletny AcceptThread

private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;
    public boolean successInit = false;

    public AcceptThread() {
        closeAllConnections();

        /*
         * if(mmServerSocket != null) { try { mmServerSocket.close(); } catch
         * (IOException e) { e.printStackTrace(); } }
         */
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        while (!successInit) {
            try {
                tmp = mAdapter
                        .listenUsingRfcommWithServiceRecord(NAME, MY_UUID);

                successInit = true;
            } catch (Exception e) {

                successInit = false;
            }
        }

        /*
         * try { tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,
         * MY_UUID); successInit= true; } catch (IOException e) { Log.e(TAG,
         * "listen() failed", e); tmp = null; successInit = false; }
         */
        mmServerSocket = tmp;
    }

    public void run() {
        if (D)
            Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mAdapter.cancelDiscovery();

                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                Log.e("Error", "This isn't connecting");
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new
                        // socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D)
            Log.i(TAG, "END mAcceptThread");
    }

    public void cancel() {
        if (D)
            Log.d(TAG, "cancel " + this);
        try {
            mmServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of server failed", e);
        }
    }
}     

Oto funkcja, którą nazywam na początkuAcceptThread w nadziei na zamknięcie wszystkiego, aby go ponownie uruchomić

public void closeAllConnections() {
    if (mmInStream != null) {
        try {mmInStream.close();}
        catch  (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
    }
    if (mmOutStream != null) {
        try {mmOutStream.close();}
        catch (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
    }
    if (mmSocket != null) {
        try {
            mmSocket.close();
            //mmSocket.connect();
        }
        catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

PrzeczytałemDokumenty Bluetooth i pytania SO, ale nie znalazłem niczego, co by dla mnie działało i trochę mnie to mylę, ponieważ po raz pierwszy łączę się przez BT.

Uwaga

Jedyną „poprawką”, jaką znalazłem, gdy to się dzieje, jest wyłączenie adaptera BT, wymuszenie zamknięcia programu, ponowne uruchomienie adaptera BT i ponowne uruchomienie aplikacji, co z oczywistych powodów nie jest dobre. Próbowałem zrestartować adapter programowo, ale nadal nie mogę się połączyć.

Czy ktoś może zobaczyć, co może być nie tak w mojej klasie BlutoothService, czyli gdzieAcceptThread jest usytuowany? A może chciałbym rozwiązać ten problem? Dzięki!

Aktualizacja

W rzeczywistości wydaje się, że połączenie jest czasami zamknięte na jednymThread i próbuje ponownie połączyć się z innym. Problem polega na tym, że nie mogę zrozumieć, co spowodowałoby, że spróbowałby połączyć się na osobnymThread lub jak to naprawić, gdy tak się stanie.

Jedynym sposobem na pomyślne odtworzenie tego stanu jest wyłączenie urządzenia BT, a następnie wyłączenie adaptera BT. Kiedy włączam wszystko z powrotem, otrzymuję wyjątek i nie mogę się połączyć. Mam klientów, którzy zdarzają się losowo i okresowo, więc mam nadzieję, że problemy są ze sobą powiązane.

questionAnswers(3)

yourAnswerToTheQuestion