Accept () mathos von Android BluetoothServerSocket löst IOException aus

Ich verwende den Beispiel-Bluetooth-Chat-Code von Google mit Änderungen, um eine Bluetooth-Chat-Anwendung zu entwickeln. Ich erhalteIOException von BluetoothServerSocket'saccept() Methode. Ich weiß nicht, warum das passiert. Kann mir jemand weiterhelfen? Ich poste einen Teil meines Codes. Jedes Mal, wenn der Flow in den Catch-Part geht. Ich habe einige Antworten gelesen, kann aber keine Hilfe bekommen.

private class AcceptThread extends Thread {
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread() {
            BluetoothServerSocket tmp = null;

            // Create a new listening server socket
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                if(tmp != null)
                    Log.d("listening", this.getId()+" listening");

            } catch (IOException e) {
                Log.e(TAG, "listen() failed", e);
            }
            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

                    socket = mmServerSocket.accept();

                } catch (IOException e) {

                        Log.d("errormsg", e.getMessage());
                        // Send a failure message back to the Activity
                        Message msg = mHandler.obtainMessage(HomepageActivity.MESSAGE_TOAST);
                        Bundle bundle = new Bundle();
                        bundle.putString(HomepageActivity.TOAST, "Remote device didn't accept the connection request");
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);
                        break;

                }

                // If a connection was accepted
                if (socket != null) {
                    synchronized (BluetoothChatService.this) {
                        switch (mState) {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            Log.d("msg", "socket not null");
                            // 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, this.getId()+"END mAcceptThread");
        }

Logcat

08-28 19:34:04.161: E/BluetoothChatService(6449): accept() failed 08-28 19:34:04.161:
 E/BluetoothChatService(6449): java.io.IOException: read failed, 
    socket might closed or timeout, read ret: -1 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
    readAll(BluetoothSocket.java:553) 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
    waitSocketSignal(BluetoothSocket.java:530) 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at 
    android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:440)

Antworten auf die Frage(0)

Ihre Antwort auf die Frage