@Test public void testSetConnectionTimeout() throws Exception { Field clientThreadsField = mBluetoothConnector.getClass().getDeclaredField("mClientThreads"); clientThreadsField.setAccessible(true); CopyOnWriteArrayList<BluetoothClientThread> myClientThreads = new CopyOnWriteArrayList<>(); Field connectionTimeoutTimerField = mBluetoothConnector.getClass() .getDeclaredField("mConnectionTimeoutTimer"); connectionTimeoutTimerField.setAccessible(true); mBluetoothConnector.setConnectionTimeout( BluetoothConnector.DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS); assertThat("No timeout is set If the given value is the same as the current one", connectionTimeoutTimerField.get(mBluetoothConnector), is(nullValue())); mBluetoothConnector.setConnectionTimeout(-1); assertThat("No timeout is set If the given value is negative", connectionTimeoutTimerField.get(mBluetoothConnector), is(nullValue())); mBluetoothConnector.setConnectionTimeout(0); assertThat("No timeout is set If the given value is 0", connectionTimeoutTimerField.get(mBluetoothConnector), is(nullValue())); connectionTimeoutTimerField.set(mBluetoothConnector, mMockConnectionTimeoutTimer); mBluetoothConnector.setConnectionTimeout(1000); assertThat("No timeout is set If the client threads is list is empty", connectionTimeoutTimerField.get(mBluetoothConnector), is(nullValue())); verify(mMockConnectionTimeoutTimer, times(1)) .cancel(); reset(mMockConnectionTimeoutTimer); myClientThreads.add(mMockBluetoothClientThread); clientThreadsField.set(mBluetoothConnector, myClientThreads); mBluetoothConnector.setConnectionTimeout(1500); assertThat("The proper timeout is set", connectionTimeoutTimerField.get(mBluetoothConnector), instanceOf(CountDownTimer.class)); verify(mMockConnectionTimeoutTimer, never()) .cancel(); }
public void setConnectionTimeout(long connectionTimeoutInMilliseconds) { if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) { Log.v(TAG, "setConnectionTimeout: " + mConnectionTimeoutInMilliseconds + " -> " + connectionTimeoutInMilliseconds); mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds; if (mConnectionTimeoutInMilliseconds > 0 && mClientThreads.size() > 0) { createConnectionTimeoutTimer(); } else { if (mConnectionTimeoutTimer != null) { mConnectionTimeoutTimer.cancel(); mConnectionTimeoutTimer = null; } } } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public void setConnectionTimeout(long connectionTimeoutInMilliseconds) { if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) { Log.v(TAG, "setConnectionTimeout: " + mConnectionTimeoutInMilliseconds + " -> " + connectionTimeoutInMilliseconds); mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds; if (mConnectionTimeoutInMilliseconds > 0 && mClientThreads.size() > 0) { createConnectionTimeoutTimer(); } else { if (mConnectionTimeoutTimer != null) { mConnectionTimeoutTimer.cancel(); mConnectionTimeoutTimer = null; } } } } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { public void setConnectionTimeout(long connectionTimeoutInMilliseconds) { if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) { Log.v(TAG, "setConnectionTimeout: " + mConnectionTimeoutInMilliseconds + " -> " + connectionTimeoutInMilliseconds); mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds; if (mConnectionTimeoutInMilliseconds > 0 && mClientThreads.size() > 0) { createConnectionTimeoutTimer(); } else { if (mConnectionTimeoutTimer != null) { mConnectionTimeoutTimer.cancel(); mConnectionTimeoutTimer = null; } } } } 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 void setConnectionTimeout(long connectionTimeoutInMilliseconds) { if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) { Log.v(TAG, "setConnectionTimeout: " + mConnectionTimeoutInMilliseconds + " -> " + connectionTimeoutInMilliseconds); mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds; if (mConnectionTimeoutInMilliseconds > 0 && mClientThreads.size() > 0) { createConnectionTimeoutTimer(); } else { if (mConnectionTimeoutTimer != null) { mConnectionTimeoutTimer.cancel(); mConnectionTimeoutTimer = null; } } } } 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 void setConnectionTimeout(long connectionTimeoutInMilliseconds) { if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) { Log.v(TAG, "setConnectionTimeout: " + mConnectionTimeoutInMilliseconds + " -> " + connectionTimeoutInMilliseconds); mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds; if (mConnectionTimeoutInMilliseconds > 0 && mClientThreads.size() > 0) { createConnectionTimeoutTimer(); } else { if (mConnectionTimeoutTimer != null) { mConnectionTimeoutTimer.cancel(); mConnectionTimeoutTimer = null; } } } } 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; }