I have a strange issue with bluetooth socket. If I create socket and later close application, android device freeze with very hight CPU load.
Here my sample code:
<I am facing the same issue but in server mode when I used BluetoothServerSocket. I was using system.exit(0) to quit the application which I read somewhere this is not recommended. I removed the system.exit(0) call and I don't have the freeze issue. (But if I kill the app then it does exhibit the freeze).
Does anybody has an issue in Server mode when sometimes the SDP record doesn't get deleted?
Did you try looking into traceview ? which method is stalling the cpu ? That may give you some insights on this problem. Even if this happens only on a particular device, you should be able to find with traceview.
A solution I found working (for a samsung galaxy mini) - that is quite unfriendly to the user, and not good "design" (but the broadcom firmware bug is not good "design" anyway) - but it's better than letting the user's phone freeze - is to turn OFF the bluetooth after we're done:
In both my onDestroy() and onBackPressed() - I call my cleanup() function that has something like this:
if(mBluetoothAdapter != null)
{
mBluetoothAdapter.disable();
}
mBluetoothAdapter = null;
Take a look at the code that calls cancel()
. In particular, look at how it handles runtime exceptions thrown by cancel()
. The code above will throw a NPE if cancel()
is called after ConnectThread()
catches an exception.
Also, check any loops where you perform an operation on socket
. Once the close()
is called, connect()
, getInputStream()
, and getOutputStream()
will throw IOEs.
OK, it's an Samsung GALAXY Tab bug, so waiting Android update...
UPDATE: fixed in new firmware!
Did the socket get successfully created before calling the Close() method ? I would try initializing the socket to null before the call to createRfcommSocketToServiceRecord(UUID) The Bluetooth Chat example does this .. here is a snippet .
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) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}