flutter_reactive_ble icon indicating copy to clipboard operation
flutter_reactive_ble copied to clipboard

After disconnecting ble device in android by cancelling the subscription the device is not showing in the respective next scan .Need to kill the app and restart

Open vivek-regalrex opened this issue 3 years ago • 3 comments

Describe the bug IOS working fine the same scenario.issue in Android After disconnecting ble device in android by cancelling the subscription the device is not showing in the respective next scan .Need to kill the app and restart

To Reproduce Steps to reproduce the behavior:

  1. Connect to device using connect method connectToDevice(
  2. disconnect using cancelling the connectToDevice subscription .cancel() method
  3. doing a scan for devices using scanForDevices (behaviour is before starting new scan we will cancel the existing subscription)

Expected behavior Device should show in next scan

  • [ ] NRF connect works fine Smartphone
  • Device: [MiA2]
  • OS: [Android 10]

Peripheral device

  • Vendor, model: [e.g. Motor Pump]
  • Does it run a custom firmware / software: yes

Additional context Add any other context about the problem here.

vivek-regalrex avatar Jun 17 '22 12:06 vivek-regalrex

In case it helps, I manually cancel both scan and connection and it's working fine in Android for me. Well I have other issues, but not this one.

  _connection = _ble
      .connectToAdvertisingDevice(
    id: device.id,
    withServices: [],
    prescanDuration: const Duration(seconds: 5),
    connectionTimeout: const Duration(seconds: 20),
  ).....
  _connection.cancel() (inside a try block in case it does not exist anymore)
  
      scanStream = _ble.scanForDevices(
    withServices: [],
    scanMode: ScanMode.balanced,
    requireLocationServicesEnabled: true).listen((scannedDevice) {....
    
    _scanStream.cancel();

pedropastor avatar Jun 20 '22 12:06 pedropastor

Please find the logs.somehow im seeing autoconnect is getting true after disconnecting the device might causing the device not to show in next scan and im providing connection timeout parameter

I/flutter (29755): disconnecting to device: 84:71:27:AA:62:D0 I/flutter (29755): disconnecting to device: 84:71:27:AA:62:D0

D/BluetoothGatt(29755): cancelOpen() - device: 84:71:27:AA:62:D0 D/BluetoothGatt(29755): onClientConnectionState() - status=0 clientIf=8 device=84:71:27:AA:62:D0 D/BluetoothGatt(29755): close() D/BluetoothGatt(29755): unregisterApp() - mClientIf=8 D/BluetoothGatt(29755): connect() - device: 84:71:27:AA:62:D0, auto: true D/BluetoothGatt(29755): registerApp()

vivek-regalrex avatar Jun 20 '22 12:06 vivek-regalrex

I had a similar problem, but with iOS devices. After connecting to the device, try to close the subscription to search for other devices, and when you disconnect from the device, start it again. Also, when disconnecting from the device, add a small delay before unsubscribing, this also helps to avoid some issues.

Example

await characteristicStream?.cancel();
await Future.delayed(const Duration(milliseconds: 300));
await deviceConnectionStateStream?.cancel();

Yuriy-Trofimov avatar Sep 14 '22 11:09 Yuriy-Trofimov

I had a similar problem, but with iOS devices. After connecting to the device, try to close the subscription to search for other devices, and when you disconnect from the device, start it again. Also, when disconnecting from the device, add a small delay before unsubscribing, this also helps to avoid some issues.

Example

await characteristicStream?.cancel();
await Future.delayed(const Duration(milliseconds: 300));
await deviceConnectionStateStream?.cancel();

This should fix your issue, I don't think this is library issues but rather an implementation issue.

Taym95 avatar Oct 24 '22 14:10 Taym95