안드로이드 블루수트 통신중에 중간에 클라가 죽습니다ㅠㅠ
조회수 1747회
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");
}
}
댓글 입력