@Test public void testOnBluetoothAdapterScanModeChanged() throws Exception { Field stateField = connectionManager.getClass().getDeclaredField("mState"); stateField.setAccessible(true); Field shouldBeStartedField = connectionManager.getClass().getDeclaredField("mShouldBeStarted"); shouldBeStartedField.setAccessible(true); shouldBeStartedField.setBoolean(connectionManager, true); int mode = BluetoothAdapter.SCAN_MODE_NONE; int state = BluetoothAdapter.STATE_OFF; when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(true); connectionManager.onBluetoothAdapterScanModeChanged(mode); verify(mMockBluetoothConnector, times(1)).stopListeningForIncomingConnections(); assertThat(connectionManager.getState(), is(ConnectionManager.ConnectionManagerState.NOT_STARTED)); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(false); connectionManager.onBluetoothAdapterStateChanged(state); assertThat(connectionManager.getState(), is(ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED)); reset(mMockBluetoothConnector); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.RUNNING); connectionManager.onBluetoothAdapterScanModeChanged(mode); verify(mMockBluetoothConnector, times(1)).stopListeningForIncomingConnections(); assertThat(connectionManager.getState(), is(ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED)); reset(mMockBluetoothConnector); connectionManager.onBluetoothAdapterScanModeChanged(mode); verify(mMockBluetoothConnector, never()).stopListeningForIncomingConnections(); assertThat(connectionManager.getState(), is(ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED)); mode = BluetoothAdapter.SCAN_MODE_CONNECTABLE; stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.NOT_STARTED); ConnectionManager connectionManagerSpy = spy(connectionManager); connectionManagerSpy.onBluetoothAdapterScanModeChanged(mode); verify(connectionManagerSpy, never()).startListeningForIncomingConnections(); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.RUNNING); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(false); connectionManagerSpy = spy(connectionManager); connectionManagerSpy.onBluetoothAdapterScanModeChanged(mode); verify(connectionManagerSpy, never()).startListeningForIncomingConnections(); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.RUNNING); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(true); connectionManagerSpy = spy(connectionManager); connectionManagerSpy.onBluetoothAdapterScanModeChanged(mode); verify(connectionManagerSpy, never()).startListeningForIncomingConnections(); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(false); connectionManagerSpy = spy(connectionManager); connectionManagerSpy.onBluetoothAdapterScanModeChanged(mode); verify(connectionManagerSpy, never()).startListeningForIncomingConnections(); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED); when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(true); connectionManagerSpy = spy(connectionManager); connectionManagerSpy.onBluetoothAdapterScanModeChanged(mode); verify(connectionManagerSpy, times(1)).startListeningForIncomingConnections(); }
@Override public void onBluetoothAdapterScanModeChanged(int mode) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Mode changed to " + mode); Log.i(TAG, "onBluetoothAdapterScanModeChanged: ConnectionManagerState is " + mState.toString()); if (mode == BluetoothAdapter.SCAN_MODE_NONE) { if (mState != ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED) { Log.w(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth disabled, pausing..."); mBluetoothConnector.stopListeningForIncomingConnections(); updateState(); } } else { if (mState == ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED && mBluetoothManager.isBluetoothEnabled()) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth enabled, restarting..."); startListeningForIncomingConnections(); } } }
ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { @Override public void onBluetoothAdapterScanModeChanged(int mode) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Mode changed to " + mode); Log.i(TAG, "onBluetoothAdapterScanModeChanged: ConnectionManagerState is " + mState.toString()); if (mode == BluetoothAdapter.SCAN_MODE_NONE) { if (mState != ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED) { Log.w(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth disabled, pausing..."); mBluetoothConnector.stopListeningForIncomingConnections(); updateState(); } } else { if (mState == ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED && mBluetoothManager.isBluetoothEnabled()) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth enabled, restarting..."); startListeningForIncomingConnections(); } } } }
ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { @Override public void onBluetoothAdapterScanModeChanged(int mode) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Mode changed to " + mode); Log.i(TAG, "onBluetoothAdapterScanModeChanged: ConnectionManagerState is " + mState.toString()); if (mode == BluetoothAdapter.SCAN_MODE_NONE) { if (mState != ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED) { Log.w(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth disabled, pausing..."); mBluetoothConnector.stopListeningForIncomingConnections(); updateState(); } } else { if (mState == ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED && mBluetoothManager.isBluetoothEnabled()) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth enabled, restarting..."); startListeningForIncomingConnections(); } } } ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName);  ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName, BluetoothManager bluetoothManager,
            SharedPreferences preferences); }
ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { @Override public void onBluetoothAdapterScanModeChanged(int mode) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Mode changed to " + mode); Log.i(TAG, "onBluetoothAdapterScanModeChanged: ConnectionManagerState is " + mState.toString()); if (mode == BluetoothAdapter.SCAN_MODE_NONE) { if (mState != ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED) { Log.w(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth disabled, pausing..."); mBluetoothConnector.stopListeningForIncomingConnections(); updateState(); } } else { if (mState == ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED && mBluetoothManager.isBluetoothEnabled()) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth enabled, restarting..."); startListeningForIncomingConnections(); } } } 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); }
ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { @Override public void onBluetoothAdapterScanModeChanged(int mode) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Mode changed to " + mode); Log.i(TAG, "onBluetoothAdapterScanModeChanged: ConnectionManagerState is " + mState.toString()); if (mode == BluetoothAdapter.SCAN_MODE_NONE) { if (mState != ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED) { Log.w(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth disabled, pausing..."); mBluetoothConnector.stopListeningForIncomingConnections(); updateState(); } } else { if (mState == ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED && mBluetoothManager.isBluetoothEnabled()) { Log.i(TAG, "onBluetoothAdapterScanModeChanged: Bluetooth enabled, restarting..."); startListeningForIncomingConnections(); } } } 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);  }