I’ve got a class which replicates the Bluetooth OBEX Protocol. This class is based on the Bluetooth Chat Example. An instance of this class is constructed in my onCreate() method
bluetoothCommunicator = new BluetoothCommunicator(BaseClass.this);
This BluetoothCommunicator class has two inner classes which extends Thread
AcceptThread and ReadInputThread
From my baseClass where the communication socket is initialized, I also have the onResume() and the onPaused() methods.
@Override
public void onResume() {
bluetoothCommunicator.resumeCommunicator();
}
This will call the resumeCommunicator method:
public void resumeCommunicator() {
Log.i("RESUME COMMUNICATOR: ", "COMMUNICATOR IS RESUMED");
if(server == null)
Log.i("Server: ", "IS NULL");
if (server == null) {
AcceptThread server = new AcceptThread();
server.start();
}
if(!server.isAlive()) {
server.start();
}
}
In this method, server is an instance of AcceptThread
But this onResume method will cause some problems for me. When I put my application in the background, and tries to open it again, the onResume method is called, but it says that the Thread already has started, and throws an exception. It sounds pretty strange that this would happend, by just looking at the code in the resumeCommunicator method.
Your thread as most likely died. A thread that has died is not alive and can not be restarted. It seems weird to have a thread for bluetooth communication in an activity. You should probably place it in a service instead.