Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Commit to #576 of Thali_CordovaPlugin: Fixed a bug that was causing s…
Browse files Browse the repository at this point in the history
…econd attempts to connect after disconnecting to fail. The bug was caused by not closing the Bluetooth server socket after a connection was established.
  • Loading branch information
tompaana committed Mar 2, 2016
1 parent 392b6cd commit b03bb8e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -267,7 +278,7 @@ private synchronized boolean removeThreadFromList(
mSocketIoThreads.remove(thread);

if (closeSocketAndStreams) {
thread.close(true);
thread.close(true, true);
}

threadRemoved = true;
Expand Down
Expand Up @@ -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() + ")");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion 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"
2 changes: 1 addition & 1 deletion NativeTestApp/app/build.gradle
Expand Up @@ -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')
}
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b03bb8e

Please sign in to comment.