@Test public void testStartListeningForIncomingConnections_failure() throws Exception { Field serverThreadField = mBluetoothConnector.getClass() .getDeclaredField("mServerThread"); serverThreadField.setAccessible(true); serverThreadField.set(mBluetoothConnector, mMockServerThread); Field serverThreadAliveField = mBluetoothConnector.getClass() .getDeclaredField("mIsServerThreadAlive"); serverThreadAliveField.setAccessible(true); Field stoppingServerField = mBluetoothConnector.getClass() .getDeclaredField("mIsStoppingServer"); stoppingServerField.setAccessible(true); serverThreadAliveField.set(mBluetoothConnector, true); stoppingServerField.set(mBluetoothConnector, true); assertThat("Is true as the connector was already running", mBluetoothConnector.startListeningForIncomingConnections(), is(true)); serverThreadAliveField.set(mBluetoothConnector, true); stoppingServerField.set(mBluetoothConnector, false); assertThat("Is true as the connector was already running", mBluetoothConnector.startListeningForIncomingConnections(), is(true)); serverThreadAliveField.set(mBluetoothConnector, false); stoppingServerField.set(mBluetoothConnector, true); mBluetoothConnector.startListeningForIncomingConnections(); assertThat("Is false as the connector is not running and stopping the server" + " thread is still ongoing", mBluetoothConnector.startListeningForIncomingConnections(), is(false)); }
public synchronized boolean startListeningForIncomingConnections() { if (!mIsServerThreadAlive && !mIsStoppingServer) { Log.i(TAG, "startListeningForIncomingConnections: Starting..."); if (mServerThread != null) { mServerThread.shutdown(); mServerThread = null; } try { mServerThread = new BluetoothServerThread( this, mBluetoothAdapter, mServiceRecordUuid, mMyBluetoothName, mMyIdentityString); } catch (NullPointerException e) { Log.e(TAG, "Failed to create the socket listener thread: " + e.getMessage(), e); mServerThread = null; } if (mServerThread != null) { mServerThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler); mServerThread.setHandshakeRequired(mHandshakeRequired); mServerThread.start(); mIsServerThreadAlive = true; mListener.onIsServerStartedChanged(true); } } else { if (mIsServerThreadAlive) { Log.d(TAG, "startListeningForIncomingConnections: Already started"); } else { Log.e(TAG, "startListeningForIncomingConnections: The process of stopping the server thread is still ongoing, please wait for the process to complete before restarting"); } } return mIsServerThreadAlive; }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public synchronized boolean startListeningForIncomingConnections() { if (!mIsServerThreadAlive && !mIsStoppingServer) { Log.i(TAG, "startListeningForIncomingConnections: Starting..."); if (mServerThread != null) { mServerThread.shutdown(); mServerThread = null; } try { mServerThread = new BluetoothServerThread( this, mBluetoothAdapter, mServiceRecordUuid, mMyBluetoothName, mMyIdentityString); } catch (NullPointerException e) { Log.e(TAG, "Failed to create the socket listener thread: " + e.getMessage(), e); mServerThread = null; } if (mServerThread != null) { mServerThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler); mServerThread.setHandshakeRequired(mHandshakeRequired); mServerThread.start(); mIsServerThreadAlive = true; mListener.onIsServerStartedChanged(true); } } else { if (mIsServerThreadAlive) { Log.d(TAG, "startListeningForIncomingConnections: Already started"); } else { Log.e(TAG, "startListeningForIncomingConnections: The process of stopping the server thread is still ongoing, please wait for the process to complete before restarting"); } } return mIsServerThreadAlive; } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public synchronized boolean startListeningForIncomingConnections() { if (!mIsServerThreadAlive && !mIsStoppingServer) { Log.i(TAG, "startListeningForIncomingConnections: Starting..."); if (mServerThread != null) { mServerThread.shutdown(); mServerThread = null; } try { mServerThread = new BluetoothServerThread( this, mBluetoothAdapter, mServiceRecordUuid, mMyBluetoothName, mMyIdentityString); } catch (NullPointerException e) { Log.e(TAG, "Failed to create the socket listener thread: " + e.getMessage(), e); mServerThread = null; } if (mServerThread != null) { mServerThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler); mServerThread.setHandshakeRequired(mHandshakeRequired); mServerThread.start(); mIsServerThreadAlive = true; mListener.onIsServerStartedChanged(true); } } else { if (mIsServerThreadAlive) { Log.d(TAG, "startListeningForIncomingConnections: Already started"); } else { Log.e(TAG, "startListeningForIncomingConnections: The process of stopping the server thread is still ongoing, please wait for the process to complete before restarting"); } } return mIsServerThreadAlive; } BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString);  BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString,
            SharedPreferences preferences); }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public synchronized boolean startListeningForIncomingConnections() { if (!mIsServerThreadAlive && !mIsStoppingServer) { Log.i(TAG, "startListeningForIncomingConnections: Starting..."); if (mServerThread != null) { mServerThread.shutdown(); mServerThread = null; } try { mServerThread = new BluetoothServerThread( this, mBluetoothAdapter, mServiceRecordUuid, mMyBluetoothName, mMyIdentityString); } catch (NullPointerException e) { Log.e(TAG, "Failed to create the socket listener thread: " + e.getMessage(), e); mServerThread = null; } if (mServerThread != null) { mServerThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler); mServerThread.setHandshakeRequired(mHandshakeRequired); mServerThread.start(); mIsServerThreadAlive = true; mListener.onIsServerStartedChanged(true); } } else { if (mIsServerThreadAlive) { Log.d(TAG, "startListeningForIncomingConnections: Already started"); } else { Log.e(TAG, "startListeningForIncomingConnections: The process of stopping the server thread is still ongoing, please wait for the process to complete before restarting"); } } return mIsServerThreadAlive; } BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString);  BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString,
            SharedPreferences preferences); void setIdentityString(String myIdentityString); void setConnectionTimeout(long connectionTimeoutInMilliseconds); void setInsecureRfcommSocketPort(int insecureRfcommSocketPort); void setMaxNumberOfOutgoingConnectionAttemptRetries(int maxNumberOfRetries); void setHandshakeRequired(boolean handshakeRequired); synchronized boolean startListeningForIncomingConnections(); synchronized void stopListeningForIncomingConnections(); synchronized void shutdown(); synchronized boolean connect(
            BluetoothDevice bluetoothDeviceToConnectTo, PeerProperties peerProperties); synchronized boolean cancelConnectionAttempt(PeerProperties peerProperties); synchronized void cancelAllConnectionAttempts(); @Override void onIncomingConnectionConnected(
            final BluetoothSocket bluetoothSocket, final PeerProperties peerProperties); @Override void onIncomingConnectionFailed(String errorMessage); @Override void onServerStopped(); @Override void onBluetoothServerSocketConsecutiveCreationFailureCountLimitExceeded(int limit); @Override void onSocketConnected(
            BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who); @Override void onHandshakeSucceeded(
            BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who); @Override void onConnectionFailed(PeerProperties peerProperties, String errorMessage, BluetoothClientThread who); }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public synchronized boolean startListeningForIncomingConnections() { if (!mIsServerThreadAlive && !mIsStoppingServer) { Log.i(TAG, "startListeningForIncomingConnections: Starting..."); if (mServerThread != null) { mServerThread.shutdown(); mServerThread = null; } try { mServerThread = new BluetoothServerThread( this, mBluetoothAdapter, mServiceRecordUuid, mMyBluetoothName, mMyIdentityString); } catch (NullPointerException e) { Log.e(TAG, "Failed to create the socket listener thread: " + e.getMessage(), e); mServerThread = null; } if (mServerThread != null) { mServerThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler); mServerThread.setHandshakeRequired(mHandshakeRequired); mServerThread.start(); mIsServerThreadAlive = true; mListener.onIsServerStartedChanged(true); } } else { if (mIsServerThreadAlive) { Log.d(TAG, "startListeningForIncomingConnections: Already started"); } else { Log.e(TAG, "startListeningForIncomingConnections: The process of stopping the server thread is still ongoing, please wait for the process to complete before restarting"); } } return mIsServerThreadAlive; } BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString);  BluetoothConnector(
            Context context, BluetoothConnectorListener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString,
            SharedPreferences preferences); void setIdentityString(String myIdentityString); void setConnectionTimeout(long connectionTimeoutInMilliseconds); void setInsecureRfcommSocketPort(int insecureRfcommSocketPort); void setMaxNumberOfOutgoingConnectionAttemptRetries(int maxNumberOfRetries); void setHandshakeRequired(boolean handshakeRequired); synchronized boolean startListeningForIncomingConnections(); synchronized void stopListeningForIncomingConnections(); synchronized void shutdown(); synchronized boolean connect(
            BluetoothDevice bluetoothDeviceToConnectTo, PeerProperties peerProperties); synchronized boolean cancelConnectionAttempt(PeerProperties peerProperties); synchronized void cancelAllConnectionAttempts(); @Override void onIncomingConnectionConnected(
            final BluetoothSocket bluetoothSocket, final PeerProperties peerProperties); @Override void onIncomingConnectionFailed(String errorMessage); @Override void onServerStopped(); @Override void onBluetoothServerSocketConsecutiveCreationFailureCountLimitExceeded(int limit); @Override void onSocketConnected(
            BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who); @Override void onHandshakeSucceeded(
            BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who); @Override void onConnectionFailed(PeerProperties peerProperties, String errorMessage, BluetoothClientThread who); static final long DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS; static final int SYSTEM_DECIDED_INSECURE_RFCOMM_SOCKET_PORT; static final int DEFAULT_ALTERNATIVE_INSECURE_RFCOMM_SOCKET_PORT; static final int DEFAULT_MAX_NUMBER_OF_RETRIES; static final boolean DEFAULT_HANDSHAKE_REQUIRED; }