Fixed some crashes when resetting bluetooth server
This commit is contained in:
parent
de917efda4
commit
d21697a31a
3 changed files with 32 additions and 13 deletions
|
|
@ -11,10 +11,10 @@ import java.util.UUID;
|
||||||
public class BluetoothClient extends Thread {
|
public class BluetoothClient extends Thread {
|
||||||
private static final Logger log = Logger.getLogger(BluetoothClient.class);
|
private static final Logger log = Logger.getLogger(BluetoothClient.class);
|
||||||
|
|
||||||
private BluetoothSocket socket;
|
protected BluetoothSocket socket;
|
||||||
private BufferedReader in;
|
protected BufferedReader in;
|
||||||
private BufferedWriter out;
|
protected BufferedWriter out;
|
||||||
private MessageHandler msgHandler;
|
protected MessageHandler msgHandler;
|
||||||
|
|
||||||
public BluetoothClient(BluetoothDevice device, String service_uuid, MessageHandler msgHandler) throws IOException {
|
public BluetoothClient(BluetoothDevice device, String service_uuid, MessageHandler msgHandler) throws IOException {
|
||||||
log.debug("Starting up bluetooth client.");
|
log.debug("Starting up bluetooth client.");
|
||||||
|
|
@ -40,7 +40,6 @@ public class BluetoothClient extends Thread {
|
||||||
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||||
}
|
}
|
||||||
protected BluetoothClient(BluetoothSocket socket, MessageHandler msgHandler) throws IOException {
|
protected BluetoothClient(BluetoothSocket socket, MessageHandler msgHandler) throws IOException {
|
||||||
log.debug("Starting up bluetooth worker thread.");
|
|
||||||
this.msgHandler = msgHandler;
|
this.msgHandler = msgHandler;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
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
|
* Will terminate an in-progress connection, and close the socket
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public synchronized void close() {
|
||||||
if(socket == null)
|
if(socket == null)
|
||||||
return;
|
return;
|
||||||
try {
|
try {
|
||||||
|
|
@ -85,6 +84,9 @@ public class BluetoothClient extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean connected(){
|
||||||
|
return socket != null;
|
||||||
|
}
|
||||||
|
|
||||||
public interface MessageHandler{
|
public interface MessageHandler{
|
||||||
public void messageReceived(String msg);
|
public void messageReceived(String msg);
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,9 @@ public class BluetoothServer extends Thread {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
BluetoothSocket socket = null;
|
|
||||||
// Keep listening until exception occurs or a socket is returned
|
// Keep listening until exception occurs or a socket is returned
|
||||||
while (serverSocket != null) {
|
while (serverSocket != null) {
|
||||||
|
BluetoothSocket socket = null;
|
||||||
try {
|
try {
|
||||||
socket = serverSocket.accept();
|
socket = serverSocket.accept();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -41,10 +41,7 @@ public class BluetoothServer extends Thread {
|
||||||
// If a connection was accepted
|
// If a connection was accepted
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
// Do work to manage the connection (in a separate thread)
|
// Do work to manage the connection (in a separate thread)
|
||||||
BluetoothClient client = new BluetoothClient(socket, msgHandler);
|
BluetoothServerWorker client = new BluetoothServerWorker(socket, msgHandler);
|
||||||
workers.put(
|
|
||||||
socket.getRemoteDevice(),
|
|
||||||
client);
|
|
||||||
client.start();
|
client.start();
|
||||||
log.debug("New Client Connection, connection list size: "+workers.size());
|
log.debug("New Client Connection, connection list size: "+workers.size());
|
||||||
}
|
}
|
||||||
|
|
@ -52,6 +49,7 @@ public class BluetoothServer extends Thread {
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.trace(null, e);
|
log.trace(null, e);
|
||||||
}
|
}
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(String msg) throws IOException {
|
public void sendMessage(String msg) throws IOException {
|
||||||
|
|
@ -63,10 +61,14 @@ public class BluetoothServer extends Thread {
|
||||||
workers.get(device).sendMessage(msg);
|
workers.get(device).sendMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getNrOfConnections(){
|
||||||
|
return workers.size();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will cancel the listening socket, and cause the thread to finish
|
* Will cancel the listening socket, and cause the thread to finish
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public synchronized void close() {
|
||||||
if(serverSocket == null)
|
if(serverSocket == null)
|
||||||
return;
|
return;
|
||||||
try {
|
try {
|
||||||
|
|
@ -76,4 +78,19 @@ public class BluetoothServer extends Thread {
|
||||||
workers.get(device).close();
|
workers.get(device).close();
|
||||||
} catch (IOException e) { }
|
} 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -305,7 +305,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
if(executor != null){
|
if(executor != null){
|
||||||
executor.terminate();
|
executor.terminateNonBlock();
|
||||||
executor.reset();
|
executor.reset();
|
||||||
executor = null;
|
executor = null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue