diff --git a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothClientThread.java b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothClientThread.java index 46ea506..017bdae 100644 --- a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothClientThread.java +++ b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothClientThread.java @@ -282,7 +282,7 @@ public void onDisconnected(String reason, BluetoothSocketIoThread who) { */ private synchronized void close() { if (mHandshakeThread != null) { - mHandshakeThread.close(false); + mHandshakeThread.close(true, false); mHandshakeThread = null; } diff --git a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothServerThread.java b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothServerThread.java index 8faa4dc..f23ee8a 100644 --- a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothServerThread.java +++ b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/internal/bluetooth/BluetoothServerThread.java @@ -46,7 +46,7 @@ public interface Listener { private final Listener mListener; private final BluetoothAdapter mBluetoothAdapter; private final String mBluetoothName; - private BluetoothServerSocket mServerSocket = null; + private BluetoothServerSocket mBluetoothServerSocket = null; private boolean mStopThread = false; /** @@ -85,17 +85,19 @@ public BluetoothServerThread( public void run() { while (!mStopThread) { try { - mServerSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(mBluetoothName, mServiceRecordUuid); + mBluetoothServerSocket = + mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord( + mBluetoothName, mServiceRecordUuid); } catch (IOException e) { Log.e(TAG, "run: Failed to start listening: " + e.getMessage(), e); } - if (mServerSocket != null) { + if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections..."); BluetoothSocket bluetoothSocket = null; try { - bluetoothSocket = mServerSocket.accept(); // Blocking call + bluetoothSocket = mBluetoothServerSocket.accept(); // Blocking call Log.i(TAG, "Incoming connection accepted"); } catch (IOException e) { if (!mStopThread) { @@ -142,8 +144,17 @@ public void run() { mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } - } - } + + try { + mBluetoothServerSocket.close(); + Log.v(TAG, "Bluetooth server socket closed"); + } catch (IOException | NullPointerException e) { + Log.e(TAG, "Failed to close the Bluetooth server socket: " + e.getMessage(), e); + } + + mBluetoothServerSocket = null; + } // if (mBluetoothServerSocket != null && !mStopThread) + } // while (!mStopThread) Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); @@ -160,17 +171,17 @@ public synchronized void shutdown() { for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { - thread.close(true); + thread.close(true, true); } } mSocketIoThreads.clear(); - if (mServerSocket != null) { + if (mBluetoothServerSocket != null) { try { - mServerSocket.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to close the socket: " + e.getMessage()); + mBluetoothServerSocket.close(); + } catch (IOException | NullPointerException e) { + Log.e(TAG, "Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } } @@ -267,7 +278,7 @@ private synchronized boolean removeThreadFromList( mSocketIoThreads.remove(thread); if (closeSocketAndStreams) { - thread.close(true); + thread.close(true, true); } threadRemoved = true; diff --git a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/utils/BluetoothSocketIoThread.java b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/utils/BluetoothSocketIoThread.java index 849d796..571bb2c 100644 --- a/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/utils/BluetoothSocketIoThread.java +++ b/BtConnectorLib/btconnectorlib2/src/main/java/org/thaliproject/p2p/btconnectorlib/utils/BluetoothSocketIoThread.java @@ -178,27 +178,31 @@ public boolean write(byte[] bytes) { } /** - * Closes the input and output streams and, if requested, the socket. + * Closes, if requested, the input and output streams and the socket. * Note that after calling this method, this instance is no longer in valid state and must be * disposed of. + * @param closeStreams If true, will close the input and output streams. * @param closeSocket If true, will close the socket. Otherwise only the streams are closed. */ - public synchronized void close(boolean closeSocket) { + public synchronized void close(boolean closeStreams, boolean closeSocket) { mIsShuttingDown = true; - if (mInputStream != null) { - try { - mInputStream.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to close the input stream: " + e.getMessage() + " (thread ID: " + getId() + ")"); + if (closeStreams) { + if (mInputStream != null) { + try { + mInputStream.close(); + } catch (IOException e) { + Log.w(TAG, "Failed to close the input stream: " + e.getMessage() + " (thread ID: " + getId() + ")"); + } } - } - if (mOutputStream != null) { - try { - mOutputStream.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to close the output stream: " + e.getMessage() + " (thread ID: " + getId() + ")"); + + if (mOutputStream != null) { + try { + mOutputStream.close(); + } catch (IOException e) { + Log.w(TAG, "Failed to close the output stream: " + e.getMessage() + " (thread ID: " + getId() + ")"); + } } } diff --git a/BtConnectorLib/settings.gradle b/BtConnectorLib/settings.gradle index 58035e2..667ab8f 100644 --- a/BtConnectorLib/settings.gradle +++ b/BtConnectorLib/settings.gradle @@ -1,3 +1,3 @@ include ':btconnectorlib2' -gradle.ext.version = "0.2.4"; +gradle.ext.version = "0.2.5"; gradle.ext.group = "org.thaliproject.p2p.btconnectorlib" diff --git a/NativeTestApp/app/build.gradle b/NativeTestApp/app/build.gradle index 5c8ce60..bed486e 100644 --- a/NativeTestApp/app/build.gradle +++ b/NativeTestApp/app/build.gradle @@ -39,5 +39,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' - compile(group: 'org.thaliproject.p2p.btconnectorlib', name: 'btconnectorlib2', version: '0.2.4', ext: 'aar') + compile(group: 'org.thaliproject.p2p.btconnectorlib', name: 'btconnectorlib2', version: '0.2.5', ext: 'aar') } diff --git a/NativeTestApp/app/src/main/java/org/thaliproject/nativetest/app/model/Connection.java b/NativeTestApp/app/src/main/java/org/thaliproject/nativetest/app/model/Connection.java index 6f0ff1b..42ba18c 100644 --- a/NativeTestApp/app/src/main/java/org/thaliproject/nativetest/app/model/Connection.java +++ b/NativeTestApp/app/src/main/java/org/thaliproject/nativetest/app/model/Connection.java @@ -1,4 +1,4 @@ -/* Copyright (c) 2015 Microsoft Corporation. This software is licensed under the MIT License. +/* Copyright (c) 2015-2016 Microsoft Corporation. This software is licensed under the MIT License. * See the license file delivered with this project for further information. */ package org.thaliproject.nativetest.app.model; @@ -155,7 +155,7 @@ public void disconnect() { public void close(boolean closeSocket) { if (!mIsClosed) { - mBluetoothSocketIoThread.close(closeSocket); + mBluetoothSocketIoThread.close(true, closeSocket); Log.d(TAG, "close: Closed"); mIsClosed = true; }