diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java index e4d3610..8fa8516 100755 --- a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java @@ -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); diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java index bf694d2..32fc6d8 100755 --- a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java @@ -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(); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/ericsson/uecontrol/gui/MainActivity.java b/app/src/main/java/com/ericsson/uecontrol/gui/MainActivity.java index ff048d1..60b464c 100755 --- a/app/src/main/java/com/ericsson/uecontrol/gui/MainActivity.java +++ b/app/src/main/java/com/ericsson/uecontrol/gui/MainActivity.java @@ -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; }