Fixed some crashes when resetting bluetooth server

This commit is contained in:
Ziver Koc 2015-02-04 17:34:08 +01:00
parent de917efda4
commit d21697a31a
3 changed files with 32 additions and 13 deletions

View file

@ -11,10 +11,10 @@ import java.util.UUID;
public class BluetoothClient extends Thread {
private static final Logger log = Logger.getLogger(BluetoothClient.class);
private BluetoothSocket socket;
private BufferedReader in;
private BufferedWriter out;
private MessageHandler msgHandler;
protected BluetoothSocket socket;
protected BufferedReader in;
protected BufferedWriter out;
protected MessageHandler msgHandler;
public BluetoothClient(BluetoothDevice device, String service_uuid, MessageHandler msgHandler) throws IOException {
log.debug("Starting up bluetooth client.");
@ -40,7 +40,6 @@ public class BluetoothClient extends Thread {
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
protected BluetoothClient(BluetoothSocket socket, MessageHandler msgHandler) throws IOException {
log.debug("Starting up bluetooth worker thread.");
this.msgHandler = msgHandler;
this.socket = socket;
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
@ -73,7 +72,7 @@ public class BluetoothClient extends Thread {
/**
* Will terminate an in-progress connection, and close the socket
*/
public void close() {
public synchronized void close() {
if(socket == null)
return;
try {
@ -85,6 +84,9 @@ public class BluetoothClient extends Thread {
}
}
public boolean connected(){
return socket != null;
}
public interface MessageHandler{
public void messageReceived(String msg);

View file

@ -30,9 +30,9 @@ public class BluetoothServer extends Thread {
public void run() {
try {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (serverSocket != null) {
BluetoothSocket socket = null;
try {
socket = serverSocket.accept();
} catch (IOException e) {
@ -41,10 +41,7 @@ public class BluetoothServer extends Thread {
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
BluetoothClient client = new BluetoothClient(socket, msgHandler);
workers.put(
socket.getRemoteDevice(),
client);
BluetoothServerWorker client = new BluetoothServerWorker(socket, msgHandler);
client.start();
log.debug("New Client Connection, connection list size: "+workers.size());
}
@ -52,6 +49,7 @@ public class BluetoothServer extends Thread {
}catch (Exception e){
log.trace(null, e);
}
close();
}
public void sendMessage(String msg) throws IOException {
@ -63,10 +61,14 @@ public class BluetoothServer extends Thread {
workers.get(device).sendMessage(msg);
}
public int getNrOfConnections(){
return workers.size();
}
/**
* Will cancel the listening socket, and cause the thread to finish
*/
public void close() {
public synchronized void close() {
if(serverSocket == null)
return;
try {
@ -76,4 +78,19 @@ public class BluetoothServer extends Thread {
workers.get(device).close();
} catch (IOException e) { }
}
protected class BluetoothServerWorker extends BluetoothClient{
protected BluetoothServerWorker(BluetoothSocket socket, MessageHandler msgHandler) throws IOException {
super(socket, msgHandler);
log.debug("Starting up bluetooth worker thread.");
workers.put( socket.getRemoteDevice(), this);
}
public synchronized void close(){
workers.remove(socket.getRemoteDevice() );
super.close();
}
}
}

View file

@ -305,7 +305,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
@Override
protected void onDestroy() {
if(executor != null){
executor.terminate();
executor.terminateNonBlock();
executor.reset();
executor = null;
}