@Test public void testOnServerStopped() throws Exception { Field serverThreadField = mBluetoothConnector.getClass() .getDeclaredField("mServerThread"); serverThreadField.setAccessible(true); serverThreadField.set(mBluetoothConnector, mMockServerThread); Field stoppingServerField = mBluetoothConnector.getClass() .getDeclaredField("mIsStoppingServer"); stoppingServerField.setAccessible(true); stoppingServerField.set(mBluetoothConnector, false); Field serverThreadAliveField = mBluetoothConnector.getClass() .getDeclaredField("mIsServerThreadAlive"); serverThreadAliveField.setAccessible(true); serverThreadAliveField.set(mBluetoothConnector, true); Field handlerField = mBluetoothConnector.getClass().getDeclaredField("mHandler"); handlerField.setAccessible(true); handlerField.set(mBluetoothConnector, mMockHandler); final ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); mBluetoothConnector.onServerStopped(); verify(mMockHandler, never()).post(captor.capture()); verify(mMockServerThread, times(1)).shutdown(); assertThat("The server is restarted", serverThreadField.get(mBluetoothConnector), is(notNullValue())); stoppingServerField.set(mBluetoothConnector, true); mBluetoothConnector.onServerStopped(); verify(mMockHandler, times(1)).post(captor.capture()); Thread thread = new Thread(captor.getValue()); thread.start(); Thread.sleep(500); verify(mMockListener, times(1)).onIsServerStartedChanged(false); assertThat("It is false as the server is stopped", (Boolean) stoppingServerField.get(mBluetoothConnector), is(false)); assertThat("It is false as the server is stopped", (Boolean) serverThreadAliveField.get(mBluetoothConnector), is(false)); }
@Override public void onServerStopped() { final boolean wasServerExplicitlyStopped = mIsStoppingServer; Log.i(TAG, "onServerStopped: Was explicitly stopped: " + wasServerExplicitlyStopped); mIsStoppingServer = false; mIsServerThreadAlive = false; if (wasServerExplicitlyStopped) { mHandler.post(new Runnable() { @Override public void run() { mListener.onIsServerStartedChanged(false); } }); } else { Log.i(TAG, "onServerStopped: Restarting the server..."); startListeningForIncomingConnections(); } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { @Override public void onServerStopped() { final boolean wasServerExplicitlyStopped = mIsStoppingServer; Log.i(TAG, "onServerStopped: Was explicitly stopped: " + wasServerExplicitlyStopped); mIsStoppingServer = false; mIsServerThreadAlive = false; if (wasServerExplicitlyStopped) { mHandler.post(new Runnable() { @Override public void run() { mListener.onIsServerStartedChanged(false); } }); } else { Log.i(TAG, "onServerStopped: Restarting the server..."); startListeningForIncomingConnections(); } } }
BluetoothConnector implements BluetoothServerThread.Listener, BluetoothClientThread.Listener { @Override public void onServerStopped() { final boolean wasServerExplicitlyStopped = mIsStoppingServer; Log.i(TAG, "onServerStopped: Was explicitly stopped: " + wasServerExplicitlyStopped); mIsStoppingServer = false; mIsServerThreadAlive = false; if (wasServerExplicitlyStopped) { mHandler.post(new Runnable() { @Override public void run() { mListener.onIsServerStartedChanged(false); } }); } else { Log.i(TAG, "onServerStopped: Restarting the server..."); startListeningForIncomingConnections(); } } 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 onServerStopped() { final boolean wasServerExplicitlyStopped = mIsStoppingServer; Log.i(TAG, "onServerStopped: Was explicitly stopped: " + wasServerExplicitlyStopped); mIsStoppingServer = false; mIsServerThreadAlive = false; if (wasServerExplicitlyStopped) { mHandler.post(new Runnable() { @Override public void run() { mListener.onIsServerStartedChanged(false); } }); } else { Log.i(TAG, "onServerStopped: Restarting the server..."); startListeningForIncomingConnections(); } } 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 onServerStopped() { final boolean wasServerExplicitlyStopped = mIsStoppingServer; Log.i(TAG, "onServerStopped: Was explicitly stopped: " + wasServerExplicitlyStopped); mIsStoppingServer = false; mIsServerThreadAlive = false; if (wasServerExplicitlyStopped) { mHandler.post(new Runnable() { @Override public void run() { mListener.onIsServerStartedChanged(false); } }); } else { Log.i(TAG, "onServerStopped: Restarting the server..."); startListeningForIncomingConnections(); } } 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; }