ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { public synchronized boolean startListeningForIncomingConnections() { Log.d(TAG, "startListeningForIncomingConnections"); switch (mState) { case NOT_STARTED: case WAITING_FOR_SERVICES_TO_BE_ENABLED: if (mBluetoothManager.bind(this)) { mShouldBeStarted = true; if (mBluetoothManager.isBluetoothEnabled()) { mBluetoothConnector.setConnectionTimeout(mSettings.getConnectionTimeout()); if (mBluetoothConnector.startListeningForIncomingConnections()) { Log.i(TAG, "startListeningForIncomingConnections: OK"); } else { Log.e(TAG, "startListeningForIncomingConnections: Failed to start"); } } else { Log.i(TAG, "startListeningForIncomingConnections: Bluetooth disabled, waiting for it to be enabled..."); updateState(); } } else { Log.e(TAG, "startListeningForIncomingConnections: Failed to start, this may indicate that Bluetooth is not supported on this device"); } break; case RUNNING: Log.d(TAG, "startListeningForIncomingConnections: Already running, call stopListeningForIncomingConnections() first in order to restart"); break; default: Log.e(TAG, "startListeningForIncomingConnections: Unrecognized state"); break; } return (mState == ConnectionManagerState.RUNNING); } ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName);  ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName, BluetoothManager bluetoothManager,
            SharedPreferences preferences); ConnectionManagerState getState(); synchronized boolean startListeningForIncomingConnections(); synchronized void stopListeningForIncomingConnections(); synchronized boolean connect(PeerProperties peerToConnectTo); synchronized boolean cancelConnectionAttempt(PeerProperties peerConnectingTo); synchronized void cancelAllConnectionAttempts(); @Override void dispose(); @Override void onConnectionManagerSettingsChanged(); @Override void onHandshakeRequiredSettingChanged(boolean handshakeRequired); @Override void onBluetoothAdapterScanModeChanged(int mode); @Override void onBluetoothAdapterStateChanged(int state); @Override void onIsServerStartedChanged(boolean isStarted); @Override void onConnecting(String bluetoothDeviceName, String bluetoothDeviceAddress); @Override void onConnected(
            final BluetoothSocket bluetoothSocket, final boolean isIncoming,
            final PeerProperties peerProperties); @Override void onConnectionTimeout(final PeerProperties peerProperties); @Override void onConnectionFailed(final PeerProperties peerProperties, final String errorMessage); @Override void onDiscoveryModeChanged(DiscoveryManager.DiscoveryMode discoveryMode, boolean startIfNotRunning); @Override void onPeerExpirationSettingChanged(long peerExpirationInMilliseconds); @Override void onAdvertiseScanSettingsChanged(); @Override void onPeerExtraInfoChanged(int newExtraInfo); @Override boolean setExtraInfo(int extraInfo);  }
@Test public void testStartListeningForIncomingConnections() throws Exception { final ConnectionManager cm = new ConnectionManager( mMockContext, mMockManagerListener, new UUID(1, 1), "testConnection", mMockBluetoothManager, mMockSharedPreferences); assertThat(cm, is(notNullValue())); when(mMockPeerProperties.toString()).thenReturn("mocked connection"); when(mMockPeerProperties.getBluetoothMacAddress()).thenReturn("01:02:03:04:05:06 "); when(mMockBluetoothManager.getRemoteDevice(anyString())).thenReturn(mBluetoothDevice); when(mMockBluetoothConnector.connect(isA(BluetoothDevice.class), isA(PeerProperties.class))).thenReturn(true); Field field = cm.getClass().getDeclaredField("mBluetoothConnector"); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(cm, mMockBluetoothConnector); when(mMockBluetoothManager.bind( isA(BluetoothManager.BluetoothManagerListener.class))).thenReturn(false); assertThat("should not start when failed to start bluetooth manager", cm.startListeningForIncomingConnections(), is(false)); assertThat("when Bluetooth is not supported", cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.NOT_STARTED))); when(mMockBluetoothManager.bind( isA(BluetoothManager.BluetoothManagerListener.class))).thenReturn(true); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(false); assertThat("should return false when Bluetooth is disabled", cm.startListeningForIncomingConnections(), is(false)); assertThat("when Bluetooth is disabled", cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState .WAITING_FOR_SERVICES_TO_BE_ENABLED))); when(mMockBluetoothManager.bind( isA(BluetoothManager.BluetoothManagerListener.class))).thenReturn(true); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(true); when(mMockBluetoothConnector.startListeningForIncomingConnections()).thenReturn(false); assertThat("should return false failed to start bluetooth connector", cm.startListeningForIncomingConnections(), is(false)); assertThat("when Bluetooth is disabled", cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState .WAITING_FOR_SERVICES_TO_BE_ENABLED))); when(mMockBluetoothConnector.startListeningForIncomingConnections()) .thenAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { cm.onIsServerStartedChanged(true); return true; } }); assertThat("should return true if running", cm.startListeningForIncomingConnections(), is(true)); assertThat("should return proper state when running", cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.RUNNING))); verify(mMockBluetoothManager, atMost(4)) .bind(isA(BluetoothManager.BluetoothManagerListener.class)); when(mMockBluetoothConnector.startListeningForIncomingConnections()).thenReturn(true); assertThat("should return true if running", cm.startListeningForIncomingConnections(), is(true)); assertThat("should return proper state when running", cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.RUNNING))); verify(mMockBluetoothManager, atMost(4)) .bind(isA(BluetoothManager.BluetoothManagerListener.class)); }