I developed an application which is connected android device with a tools via Bluetooth. I already make a connection between them, but I have some problem with IOException. It says that error is operation canceled. This is a short of my program.
protected void connect(BluetoothDevice device) {
try {
Log.d(TAG, "³¢ÊÔÁ¬½Ó");
// Create a Socket connection: need the server's UUID number of
// registered
Method m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
Log.d(TAG, ">>Client connectted");
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
final byte[] bytes = new byte[2048];
int read = -1;
while (true) {
synchronized (obj1) {
Log.d("masuk while","masuk");
// I got the problem here ----!!!!!
read = inputStream.read(bytes);
// -------------------END -----------
Log.d(TAG, "read:" + read);
if (read > 0) {
final int count = read;
String str = SamplesUtils.byteToHex(bytes, count);
// Log.d(TAG, "test1:" + str);
String hex = hexString.toString();
if (hex == "") {
hexString.append("<--");
} else {
if (hex.lastIndexOf("<--") < hex.lastIndexOf("-->")) {
hexString.append("\n<--");
}
}
hexString.append(str);
hex = hexString.toString();
Log.d(TAG, "test2:" + hex);
if (hex.length() > maxlength) {
try {
hex = hex.substring(hex.length() - maxlength,
hex.length());
hex = hex.substring(hex.indexOf(" "));
hex = "<--" + hex;
hexString = new StringBuffer();
hexString.append(hex);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "e", e);
}
}
_handler.post(new Runnable() {
public void run() {
if (cbxHexView.isChecked()) {
sTextView.setText(hexString.toString());
} else {
//if(!bufferStrToHex(hexString.toString(),false).contains("--")){
sTextView
.setText(bufferStrToHex(
hexString.toString(), false)
.trim());
temp.put(idxTemp, bufferStrToHex(hexString.toString(),false));
idxTemp++;
}
Log.d(TAG, "ScrollY: " + mScrollView.getScrollY());
int off = sTextView.getMeasuredHeight() - mScrollView.getHeight();
if (off > 0) {
mScrollView.scrollTo(0, off);
}
}
});
}
}
}
} catch (Exception e) {
Log.e(TAG, ">>", e);
Log.d("error catch:",e.toString());
return;
} finally {
if (socket != null) {
try {
Log.d(TAG, ">>Client Socket Close");
socket.close();
socket = null;
this.finish();
return;
} catch (IOException e) {
Log.e(TAG, ">>", e);
}
}
}
}
Actually I have tried before using android version about 2.x or 3.x and it is work. But, I try using 4.0.4, the problem is occur. I got confused about this. Thanks for the help.
--UPDATE LOG CAT--- Here is log cat of my running program
04-18 13:07:08.562: E/MonitorActivity(7448): >>
04-18 13:07:08.562: E/MonitorActivity(7448): java.io.IOException: Operation Canceled
04-18 13:07:08.562: E/MonitorActivity(7448): at android.bluetooth.BluetoothSocket.readNative(Native Method)
04-18 13:07:08.562: E/MonitorActivity(7448): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
04-18 13:07:08.562: E/MonitorActivity(7448): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
04-18 13:07:08.562: E/MonitorActivity(7448): at java.io.InputStream.read(InputStream.java:163)
04-18 13:07:08.562: E/MonitorActivity(7448): at com.iteadstudio.MonitorActivity.connect(MonitorActivity.java:320)
04-18 13:07:08.562: E/MonitorActivity(7448): at com.iteadstudio.MonitorActivity$3.run(MonitorActivity.java:135)
04-18 13:07:08.562: D/error catch:(7448): java.io.IOException: Operation Canceled
04-18 13:07:08.562: D/MonitorActivity(7448): >>Client Socket Close
04-18 13:07:08.572: A/libc(7448): @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree
04-18 13:07:08.572: A/libc(7448): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)
when I clicked the error, it direct to the statement which I have mentioned above. on read = inputstream.read(bytes); Hopely it is going to be clear to help.