안드로이드 블루수트 통신중에 중간에 클라가 죽습니다ㅠㅠ

조회수 1746회

W/System.err: java.io.IOException: bt socket closed, read return: -1

W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:434)

W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)

W/System.err: at java.io.InputStreamReader.read(InputStreamReader.java:231)

W/System.err: at java.io.BufferedReader.fillBuf(BufferedReader.java:145)

W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:397)

W/System.err: at com.example.lg.scoreboardapp.MainActivity$ConnectThread.run(MainActivity.java:336)

W/System.err: at java.lang.Thread.run(Thread.java:818)

안드로이드에서 하나의 서버와 두개의 클라를 해서 총 세개가 통신을 블루투스로 합니다. 클라끼리는 관련이 없고 오로지 서버하고 연결이 되서 서버로부터 데이터를 받기만 합니다. 그런데 서버에서 데이터를 보내다가 어느 순간 하나의 클라가 위에 나와있는 에러를 표시해주며 연결이 끊깁니다.... 도저히 원인이 무엇인지를 모르겠어서 여기에 여쭙니다ㅠㅠ

밑에 코드는 클라에서 쓰는 커넥트 쓰레드 코드입니다... 살려주세여....

private class ConnectThread extends Thread {
    private BluetoothSocket socket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Toast.makeText(MainActivity.this, "연결에 실패하였습니다.\n다시 시도하여 주세요", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            //mkmsg("Client connection failed: "+e.getMessage().toString()+"\n");
        }
        socket = tmp;

    }

    public void run() {
        // mkmsg("Client running\n");
        // Always cancel discovery because it will slow down a connection
        mBluetoothAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            socket.connect();
        } catch (IOException e) {
            //mkmsg("Connect failed\n");
            e.printStackTrace();
            try {
                socket.close();
                socket = null;
            } catch (IOException e2) {
                //mkmsg("unable to close() socket during connection failure: "+e2.getMessage().toString()+"\n");
                socket = null;
                e2.printStackTrace();
            }
            // Start the service over to restart listening mode
        }
        // If a connection was accepted
        if (socket != null) {
            //mkmsg("Connection made\n");
            //mkmsg("Remote device address: "+socket.getRemoteDevice().getAddress().toString()+"\n");
            //Note this is copied from the TCPdemo code.
            try {
                while(socket != null){
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    final String str1 = in.readLine();     //<<<----------에러떠있는 MainActivity .java:336
                    json1 = str1;
                    //parse();
                }


            } catch(Exception e) {
                //mkmsg("Error happened sending/receiving\n");
                e.printStackTrace();


            }
        } else {
            //mkmsg("Made connection, but socket is null\n");
        }


    }
  • 이런 런타임 에러는 글로 해결이 어렵습니다. 더구나 쓰레드 문제군요. 서버에서는 제대로 문자열을 보내나요? readLine 은 block 시키는 메소드입니다. 정영훈 2017.8.16 04:17
  • 문자열은 제대로 날아가네여ㅠㅠㅠ 쓰레드가 어떤 문제인건가여...? block시킨다는게 어떤 의미인건지...? 김정빈 2017.8.16 10:18
  • readline 은 입력이 올때까지 쓰레드를 중지시켜요. 즉 서버에서 문자열을 보내지 않는다면 가만히 중지하고 있을겁니다. 서버에서 write 후에 flush 호출도 하고 있겠죠? 정영훈 2017.8.16 11:28

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)