I’m trying to disconnect from my Bluetooth device as gracefully as possible, but Android insists on throwing me a system error:
09-02 15:16:16.748: W/System.err(31038): java.io.IOException: Operation Canceled
09-02 15:16:16.748: W/System.err(31038): at android.bluetooth.BluetoothSocket.readNative(Native Method)
09-02 15:16:16.748: W/System.err(31038): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
09-02 15:16:16.748: W/System.err(31038): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
09-02 15:16:16.748: W/System.err(31038): at com.bluetooth.BluetoothRemoteControlApp$ConnectedThread.run(BluetoothRemoteControlApp.java:207)
I’m using a modified version of BluetoothChat sample program and my connect thread goes like this:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
connected = true;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
byte ch;
while(connected) {
try {
bytes = 0;
while((ch = (byte) mmInStream.read()) != '\n')
{
buffer[bytes++] = ch;
}
busy = false;
String msg = new String(buffer, "UTF-8").substring(0, bytes - 1);
Log.d(TAG, "Read: " + msg);
if(activityHandler != null) {
activityHandler.obtainMessage(BT_READ, bytes, 0, msg).sendToTarget();
}
}
catch(IOException e) {
//e.printStackTrace();
Log.w(TAG, "Cancelling");
break;
}
}
}
public void write(byte[] buffer) {
// . . .
}
public void cancel() {
Log.i(TAG, "Cancelling connected thread");
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
During the disconnection I call the cancel method which throws the system error. Should I be worried and try to fix it? I don’t really like seeing errors on my log…
I dont think you should be too worried about this log, since you are cancelling and the log itself says
Operation Cancelled.I think you are printing the log with e.printStacktrace in your run method. That is why you are seeing this log.