BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public synchronized boolean cancelConnectionAttempt(PeerProperties peerProperties) { boolean isCancelling = false; if (peerProperties != null && mClientThreads.size() > 0) { Log.i(TAG, "cancelConnectionAttempt: " + peerProperties.toString()); BluetoothClientThread bluetoothClientThread = null; for (BluetoothClientThread currentBluetoothSocketThread : mClientThreads) { if (currentBluetoothSocketThread != null && currentBluetoothSocketThread.getPeerProperties() != null && currentBluetoothSocketThread.getPeerProperties().equals(peerProperties)) { bluetoothClientThread = currentBluetoothSocketThread; break; } } if (bluetoothClientThread != null) { isCancelling = removeAndShutdownBluetoothClientThread(bluetoothClientThread); } } else { if (peerProperties == null) { throw new NullPointerException("The given peer properties instance is null"); } else { Log.e(TAG, "cancelConnectionAttempt: No client threads to cancel"); } } return isCancelling; } 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; }
@Test public void testCancelConnectionAttempt_exception() throws Exception { thrown.expect(NullPointerException.class); mBluetoothConnector.cancelConnectionAttempt(null); }
@Test public void testCancelConnectionAttempt() throws Exception { Field connectionTimeoutTimerField = mBluetoothConnector.getClass() .getDeclaredField("mConnectionTimeoutTimer"); connectionTimeoutTimerField.setAccessible(true); connectionTimeoutTimerField.set(mBluetoothConnector, mMockConnectionTimeoutTimer); Field clientThreadsField = mBluetoothConnector.getClass().getDeclaredField("mClientThreads"); clientThreadsField.setAccessible(true); CopyOnWriteArrayList<BluetoothClientThread> myClientThreads = new CopyOnWriteArrayList<>(); myClientThreads.add(mMockBluetoothClientThread); clientThreadsField.set(mBluetoothConnector, myClientThreads); assertThat("Is false as a client thread associated with the given peer properties not found", mBluetoothConnector.cancelConnectionAttempt(mMockPeerProperties), is(false)); when(mMockBluetoothClientThread.getPeerProperties()).thenReturn(mMockPeerProperties); when(mMockBluetoothClientThread.getId()).thenReturn(123456789L); assertThat("Is true as a client thread associated with the given peer properties was found", mBluetoothConnector.cancelConnectionAttempt(mMockPeerProperties), is(true)); verify(mMockConnectionTimeoutTimer, times(1)) .cancel(); Thread.sleep(500); verify(mMockBluetoothClientThread, times(1)) .shutdown(); }