PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
661 stars 321 forks source link

Performance issue with connectToAdvertisingDevice #873

Open maBarabas opened 2 months ago

maBarabas commented 2 months ago

Describe the bug

connectToAdvertisingDevice always waits for prescanDuration before attempting to connect to a device. It causes every connection attempt to take at least prescanDuration.

The scan sessions is set up to end in prescanDuration here: https://github.com/PhilipsHue/flutter_reactive_ble/blame/96e083ac9edd2b6cb070f6534bb2309057257aa0/packages/flutter_reactive_ble/lib/src/device_connector.dart#L127, and the return below waits for the entire session to end before deciding if it should connect to the device or not.

If the device is scanned before prescanDuration, then we should connect to it immediately, instead of waiting out the prescanDuration to speed up connections.

To Reproduce Steps to reproduce the behavior:

  1. Start a stopwatch
  2. Connect to one device using connectToAdvertisingDevice(), prescanDuration = 5 seconds
  3. Wait until connected
  4. End stopwatch

Observe measured time is about 5 seconds.

Expected behavior A clear and concise description of what you expected to happen.

If the device is scanned in 100ms, then start connecting after 100ms instead of waiting for the full prescanDuration.

Smartphone / tablet

Peripheral device

Additional context Add any other context about the problem here.