@Test public void testOnHandshakeSucceeded() throws Exception { Field connectionTimeoutTimerField = mBluetoothConnector.getClass() .getDeclaredField("mConnectionTimeoutTimer"); connectionTimeoutTimerField.setAccessible(true); connectionTimeoutTimerField.set(mBluetoothConnector, mMockConnectionTimeoutTimer); Field handlerField = mBluetoothConnector.getClass().getDeclaredField("mHandler"); handlerField.setAccessible(true); handlerField.set(mBluetoothConnector, mMockHandler); Field clientThreadsField = mBluetoothConnector.getClass().getDeclaredField("mClientThreads"); clientThreadsField.setAccessible(true); CopyOnWriteArrayList<BluetoothClientThread> myClientThreads = new CopyOnWriteArrayList<>(); myClientThreads.add(mMockBluetoothClientThread); clientThreadsField.set(mBluetoothConnector, myClientThreads); Field shuttingDownField = mBluetoothConnector.getClass() .getDeclaredField("mIsShuttingDown"); shuttingDownField.setAccessible(true); shuttingDownField.set(mBluetoothConnector, true); when(mMockBluetoothSocket.isConnected()).thenReturn(true); final ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); when(mMockBluetoothClientThread.getHandshakeRequired()).thenReturn(false); shuttingDownField.set(mBluetoothConnector, false); mBluetoothConnector.onHandshakeSucceeded(mMockBluetoothSocket, mMockPeerProperties, mMockBluetoothClientThread); verify(mMockConnectionTimeoutTimer, times(1)).cancel(); assertThat("No timeout is set If the client threads is list is empty", connectionTimeoutTimerField.get(mBluetoothConnector), is(nullValue())); verify(mMockHandler, times(1)).post(captor.capture()); Thread thread = new Thread(captor.getValue()); thread.start(); Thread.sleep(500); verify(mMockListener, times(1)).onConnected(mMockBluetoothSocket, false, mMockPeerProperties); }
@Override public void onHandshakeSucceeded( BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who) { Log.i(TAG, "onHandshakeSucceeded: " + peerProperties.toString() + " (thread ID: " + who.getId() + ")"); handleSuccessfulClientThread(who, bluetoothSocket, peerProperties); }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { @Override public void onHandshakeSucceeded( BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who) { Log.i(TAG, "onHandshakeSucceeded: " + peerProperties.toString() + " (thread ID: " + who.getId() + ")"); handleSuccessfulClientThread(who, bluetoothSocket, peerProperties); } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { @Override public void onHandshakeSucceeded( BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who) { Log.i(TAG, "onHandshakeSucceeded: " + peerProperties.toString() + " (thread ID: " + who.getId() + ")"); handleSuccessfulClientThread(who, bluetoothSocket, peerProperties); } 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 { @Override public void onHandshakeSucceeded( BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who) { Log.i(TAG, "onHandshakeSucceeded: " + peerProperties.toString() + " (thread ID: " + who.getId() + ")"); handleSuccessfulClientThread(who, bluetoothSocket, peerProperties); } 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 { @Override public void onHandshakeSucceeded( BluetoothSocket bluetoothSocket, PeerProperties peerProperties, BluetoothClientThread who) { Log.i(TAG, "onHandshakeSucceeded: " + peerProperties.toString() + " (thread ID: " + who.getId() + ")"); handleSuccessfulClientThread(who, bluetoothSocket, peerProperties); } 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; }