Skip to content

Commit

Permalink
Don't stop scan before (dis)connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Dec 30, 2021
1 parent bec733c commit d9285c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void connected(@NotNull final BluetoothPeripheral peripheral) {

@Override
public void servicesDiscovered(@NotNull final BluetoothPeripheral peripheral) {
restartScannerIfNeeded();
//restartScannerIfNeeded();
}

@Override
Expand All @@ -170,7 +170,7 @@ public void connectFailed(@NotNull final BluetoothPeripheral peripheral, @NotNul

callBackHandler.post(() -> bluetoothCentralManagerCallback.onConnectionFailed(peripheral, status));

restartScannerIfNeeded();
// restartScannerIfNeeded();
}

@Override
Expand All @@ -189,14 +189,14 @@ public void disconnected(@NotNull final BluetoothPeripheral peripheral, @NotNull

callBackHandler.post(() -> bluetoothCentralManagerCallback.onDisconnectedPeripheral(peripheral, status));

restartScannerIfNeeded();
// restartScannerIfNeeded();
}

private void restartScannerIfNeeded() {
if (autoScanActive || normalScanActive) {
startScanning();
}
}
// private void restartScannerIfNeeded() {
// if (autoScanActive || normalScanActive) {
// startScanning();
// }
// }

private void completeConnectOrDisconnectCommand(@NotNull final String deviceAddress) {
// Complete the 'connect' command if this was the device we were connecting
Expand Down Expand Up @@ -427,8 +427,6 @@ private void onFoundReconnectionPeripheral(@NotNull final BluetoothPeripheral pe

logger.info(String.format("found peripheral to autoconnect '%s'", peripheralAddress));
autoScanActive = false;
stopScanning();

reconnectPeripheralAddresses.remove(peripheralAddress);
reconnectCallbacks.remove(peripheralAddress);
unconnectedPeripherals.remove(peripheralAddress);
Expand Down Expand Up @@ -892,17 +890,8 @@ public void connectPeripheral(@NotNull final BluetoothPeripheral peripheral, @No
return;
}

// Some adapters have issues with (dis)connecting while scanning, so stop scan first
stopScanning();

unconnectedPeripherals.put(peripheral.getAddress(), peripheral);
final boolean result = commandQueue.add(() -> {
try {
sleep(CONNECT_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Refresh BluezDevice because it may be old
scannedBluezDevices.remove(adapter.getPath(peripheral.getAddress()));
final BluezDevice bluezDevice = getDeviceByAddress(peripheral.getAddress());
Expand Down Expand Up @@ -992,9 +981,6 @@ public void cancelConnection(@NotNull final BluetoothPeripheral peripheral) {
Objects.requireNonNull(peripheral, NULL_PERIPHERAL_ERROR);

if (peripheral.getState() == CONNECTED) {
// Some adapters have issues with (dis)connecting while scanning, so stop scan first
stopScanning();

// Queue the low level disconnect
final boolean result = commandQueue.add(() -> {
currentDeviceAddress = peripheral.getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,22 +605,6 @@ void When_scanning_for_names_and_a_non_matching_PropertiesChanged_signal_comes_i
verify(callback, timeout(TIMEOUT_THRESHOLD).times(0)).onDiscoveredPeripheral(any(), any());
}

@Test
void Given_a_scan_is_running_when_connectionPeripheral_is_called_then_the_scan_is_stopped() throws DBusException, InterruptedException {
// Given
when(bluezAdapter.getPath(DUMMY_MAC_ADDRESS_BLP)).thenReturn(DUMMY_MAC_ADDRESS_PATH_BLP);
when(bluezAdapter.getBluezDeviceByPath(DUMMY_MAC_ADDRESS_PATH_BLP)).thenReturn(bluezDevice);
BluetoothCentralManager central = startUnfilteredScan();
when(bluezAdapter.isDiscovering()).thenReturn(true);

// When
BluetoothPeripheral peripheral = central.getPeripheral(DUMMY_MAC_ADDRESS_BLP);
central.connectPeripheral(peripheral, peripheralCallback);

// Then
verify(bluezAdapter, timeout(TIMEOUT_THRESHOLD)).stopDiscovery();
}

@Test
void When_connectPeripheral_is_called_then_a_connection_attempt_is_done_after_a_delay() throws BluezFailedException, BluezAlreadyConnectedException, BluezNotReadyException, BluezInProgressException, InterruptedException {
// Given
Expand Down Expand Up @@ -654,25 +638,7 @@ void Given_a_disconnected_peripheral_when_a_peripherals_connects_then_onConnecte
// Then
verify(callback, timeout(TIMEOUT_THRESHOLD)).onConnectedPeripheral(peripheral);
}

@Test
void Given_a_connected_peripheral_and_scanning_when_cancelPeripheralConnection_is_called_then_the_scan_is_stopped() throws DBusException, InterruptedException {
// Given
BluetoothCentralManager central = getCentral();

when(bluezAdapter.getPath(DUMMY_MAC_ADDRESS_BLP)).thenReturn(DUMMY_MAC_ADDRESS_PATH_BLP);
when(bluezAdapter.getBluezDeviceByPath(DUMMY_MAC_ADDRESS_PATH_BLP)).thenReturn(bluezDevice);
BluetoothPeripheral peripheral = central.getPeripheral(DUMMY_MAC_ADDRESS_BLP);
connectPeripheral(central, peripheral);
startScan(central);

// When
central.cancelConnection(peripheral);

// Then
verify(bluezAdapter, timeout(TIMEOUT_THRESHOLD)).stopDiscovery();
}


@Test
void Given_a_connected_peripheral_when_cancelPeripheralConnection_is_called_then_the_peripheral_is_disconnected() throws DBusException, InterruptedException {
// Given
Expand Down Expand Up @@ -754,19 +720,12 @@ void Given_an_autoConnect_is_issued_when_the_peripheral_is_seen_then_a_connect_i
verify(bluezAdapter, timeout(TIMEOUT_THRESHOLD)).startDiscovery();
central.handleSignal(getPropertiesChangeSignalDiscoveryStarted());
verify(callback, timeout(TIMEOUT_THRESHOLD)).onScanStarted();
when(bluezAdapter.isDiscovering()).thenReturn(true);

// When
central.handleSignal(getPropertiesChangedSignalWhileScanning());

// Then
verify(bluezAdapter, timeout(TIMEOUT_THRESHOLD)).stopDiscovery();

central.handleSignal(getPropertiesChangeSignalDiscoveryStopped());
when(bluezAdapter.isDiscovering()).thenReturn(false);

verify(bluezDevice, timeout(TIMEOUT_THRESHOLD)).connect();

assertFalse(central.reconnectPeripheralAddresses.contains(peripheral.getAddress()));
assertNull(central.reconnectCallbacks.get(peripheral.getAddress()));
}
Expand Down

0 comments on commit d9285c9

Please sign in to comment.