BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public void setHandshakeRequired(boolean handshakeRequired) { if (mHandshakeRequired != handshakeRequired) { Log.v(TAG, "setHandshakeRequired: " + mHandshakeRequired + " -> " + handshakeRequired); mHandshakeRequired = handshakeRequired; if (mIsServerThreadAlive) { stopListeningForIncomingConnections(); new Handler().postDelayed(new Runnable() { public void run() { startListeningForIncomingConnections(); } }, SERVER_RESTART_DELAY_IN_MILLISECONDS); } } } 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 testSetHandshakeRequired() throws Exception { Field serverThreadField = mBluetoothConnector.getClass() .getDeclaredField("mServerThread"); serverThreadField.setAccessible(true); serverThreadField.set(mBluetoothConnector, mMockServerThread); Field serverThreadAliveField = mBluetoothConnector.getClass() .getDeclaredField("mIsServerThreadAlive"); serverThreadAliveField.setAccessible(true); serverThreadAliveField.set(mBluetoothConnector, false); Field handshakeRequiredField = mBluetoothConnector.getClass() .getDeclaredField("mHandshakeRequired"); handshakeRequiredField.setAccessible(true); Field stoppingServerField = mBluetoothConnector.getClass() .getDeclaredField("mIsStoppingServer"); stoppingServerField.setAccessible(true); mBluetoothConnector.setHandshakeRequired(!BluetoothConnector.DEFAULT_HANDSHAKE_REQUIRED); verify(mMockServerThread, never()) .shutdown(); assertThat("The HandshakeRequired is properly set", (Boolean) handshakeRequiredField.get(mBluetoothConnector), is(!BluetoothConnector.DEFAULT_HANDSHAKE_REQUIRED)); serverThreadAliveField.set(mBluetoothConnector, true); mBluetoothConnector.setHandshakeRequired(BluetoothConnector.DEFAULT_HANDSHAKE_REQUIRED); assertThat("The HandshakeRequired is properly set", (Boolean) handshakeRequiredField.get(mBluetoothConnector), is(BluetoothConnector.DEFAULT_HANDSHAKE_REQUIRED)); verify(mMockServerThread, times(1)) .shutdown(); assertThat("The mIsStoppingServeris properly set", (Boolean) stoppingServerField.get(mBluetoothConnector), is(true)); assertThat("The server thread is set to null", serverThreadField.get(mBluetoothConnector), is(nullValue())); }