-1

Bluetooth ソケットに問題があります。次のメッセージが表示されますIOExceptionSocket Closed

ここで例外が発生します。

} catch (IOException e) {
        try {
          inputStream.close(); 
          socket.close();
        } catch (IOException e1) {
             Log.e("CLOSE: ", e.getMessage());
        } 
        Log.e("IO FROM RUN: ", e.getMessage());
}

関連するコードスニペット:

class AcceptThread extends Thread {
    /**
     * Tag that will appear in the log.
     */
    private final String ACCEPT_TAG = AcceptThread.class.getName();

    /**
     * The bluetooth server socket.
     */
    private final BluetoothServerSocket mServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(
                    "Bluetooth Service", UUID.fromString(defaultUUID));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        while (true) {
            try {
                Log.i(ACCEPT_TAG, "Listening for a connection...");

                socket = mServerSocket.accept();
                Log.i(ACCEPT_TAG, "Connected to "
                        + socket.getRemoteDevice().getName());

            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {

                ReadInputThread inputThread = new ReadInputThread(socket); 
                inputThread.start();

            }
        }


    }

    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mServerSocket.close();
        } catch (IOException e) {
        }
    }

    class ReadInputThread extends Thread {

        BluetoothSocket socket;
        InputStream inputStream;

        public ReadInputThread(BluetoothSocket socket) {
            InputStream tempIs = null;
            try {
                this.socket = socket;
                tempIs = socket.getInputStream();
            } catch (IOException e) {
                Log.e("IO: ", e.getMessage());
            }

            inputStream = tempIs;
        }

        public void run() {

            Log.i(TAG, "BEGIN ReadInputThread");
            final byte[] buffer = new byte[1024];
            int bytes;

            while (true) {

                try {
                    bytes = inputStream.read(buffer);
                    if(inputStream.available() < 0) {
                        inputStream.close();
                        socket.close();
                    }

                    Log.i("BYTE COUNT: ", Integer.toString(bytes)); 
                    Log.i("BYTES: ", new String(buffer)); 

                } catch (IOException e) {
                    try {
                        inputStream.close(); 
                        socket.close();
                    } catch (IOException e1) {
                    Log.e("CLOSE: ", e.getMessage());
                    } 
                    Log.e("IO FROM RUN: ", e.getMessage());
                }
            }


        }
    }

この問題を解決する方法についてのヒントを教えてもらえますか? ありがとう!

4

1 に答える 1

0

問題は次のコードにあるようです。

while (true) {    
try {
                bytes = inputStream.read(buffer);
                if(inputStream.available() < 0) {
                    inputStream.close();
                    socket.close();
                }

                Log.i("BYTE COUNT: ", Integer.toString(bytes)); 
                Log.i("BYTES: ", new String(buffer)); 

            } catch (IOException e) {
                try {
                    inputStream.close(); 
                    socket.close();
                } catch (IOException e1) {
                Log.e("CLOSE: ", e.getMessage());
                } 
                Log.e("IO FROM RUN: ", e.getMessage());
            }
        }

while ループでは、最初に利用可能なものを読み込もうとしています。利用可能なものがない場合は、ソケットを閉じています。その後、ループが繰り返され、ソケットから再度読み取ろうとしますが、閉じて、IOException が発生します。読み取るデータがない場合にループを終了する場合は、socket.close() の後に return または break を必ず追加してください。

于 2012-10-17T10:21:54.970 に答える