마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

블루투스 소켓부분 에러 - 로그확인 좀 부탁드립니다.

0 추천

안드로이드에서 제공하는 블루투스 예제를 가져와서 어플을 만들었는데 다만들고 보니 치명적인 에러가 있네요;;

처음 다운받거나 프로세스를 죽이면 정상동작하는데, 뒤로가기를 눌러 재 실행하면 이상하게 동작합니다.

개발중에 에러로그를 봤는데 동작하는데 별 문제 없길래 대수롭지 않게 넘겼는데, 이런 심각한 문제가 있네요;; 소켓을 잘 닫지 않아서 그런거라고 하는데 어디에 어떤코드를 넣어줘야할지 모르겠습니다.

여기저기 검색하고 삽질해가며 개발중인데 이 부분은 감이 안잡히네요 ㅠㅠ 도와주세요 ㅠㅠ

09-14 22:29:05.739  11758-11775/kr.hstec.bluewingsa E/SEOK﹕ accept() failed
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1
            at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:625)
            at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:602)
            at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:419)
            at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:131)
            at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:117)
            at kr.hstec.bluewingsa.BluetoothService$AcceptThread.run(BluetoothService.java:297)

아래 소스에서 tmp에 null 값이 들어오는거 같은데 이유를 모르겠습니다 ㅠㅠ

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

        public void run() {
            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.e(TAG, "accept() failed", e);
                    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;
                        }
                    }
                }
            }
            Log.i(TAG, "END mAcceptThread");

        }

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

 

09-14 22:29:06.009  11758-12152/kr.hstec.bluewingsa E/SEOK﹕ disconnected
    java.io.IOException: bt socket closed, read return: -1
            at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:526)
            at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
            at java.io.InputStream.read(InputStream.java:162)
           at kr.hstec.bluewingsa.BluetoothService$ConnectedThread.run(BluetoothService.java:440)

 

아래에 bytes =  mmInStream.read(buffer); 에서 에러입니다.

private class ConnectedThread extends Thread {
   ...
    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes = 0;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothService.this.start();
                break;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     *
     * @param buffer The bytes to write
     */
    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}
처진소나무 님이 2015년 9월 14일 질문
http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3/25647197#25647197
이걸로 어떻게 해결 안될까요? 지금이야 어떻게든 해결하셨겠지만.. ^^

1개의 답변

0 추천
로그에 적힌 그대로입니다.
연결 해제로 인해 소켓이 닫혔어요.
소켓을 다시 여세요.
Jinthree (8,980 포인트) 님이 2015년 9월 15일 답변
두번째꺼는 소켓이 닫힌건데 첫번째 에러로 인해 닫힌거 아닌가요?
첫번째 에러를 수정할 방법이 궁금합니다 ㅠㅠ
...