Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connectToAdvertisingDevice: Check for recently discovered device when BLE scan already running #875

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions packages/flutter_reactive_ble/lib/src/device_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ class DeviceConnectorImpl implements DeviceConnector {
Map<Uuid, List<Uuid>>? servicesWithCharacteristicsToDiscover,
Duration? connectionTimeout,
}) {
if (_deviceScanner.currentScan != null) {
if (_deviceIsDiscoveredRecently(
Copy link
Collaborator

@Taym95 Taym95 Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you just stop the scan and connect with connectToAdvertisingDevice ? why do we need this in library?

deviceId: id,
cacheValidity: _scanRegistryCacheValidityPeriod,
)) {
return connect(
id: id,
servicesWithCharacteristicsToDiscover:
servicesWithCharacteristicsToDiscover,
connectionTimeout: connectionTimeout,
);
} else if (_deviceScanner.currentScan != null) {
return _awaitCurrentScanAndConnect(
withServices,
prescanDuration,
Expand All @@ -111,48 +121,38 @@ class DeviceConnectorImpl implements DeviceConnector {
List<Uuid> withServices,
Duration prescanDuration,
) {
if (_deviceIsDiscoveredRecently(
deviceId: id, cacheValidity: _scanRegistryCacheValidityPeriod)) {
return connect(
id: id,
servicesWithCharacteristicsToDiscover:
servicesWithCharacteristicsToDiscover,
connectionTimeout: connectionTimeout,
);
} else {
final scanSubscription = _deviceScanner
.scanForDevices(
withServices: withServices, scanMode: ScanMode.lowLatency)
.listen((DiscoveredDevice scanData) {}, onError: (Object _) {});
Future<void>.delayed(prescanDuration).then<void>((_) {
scanSubscription.cancel();
});
final scanSubscription = _deviceScanner
.scanForDevices(
withServices: withServices, scanMode: ScanMode.lowLatency)
.listen((DiscoveredDevice scanData) {}, onError: (Object _) {});
Future<void>.delayed(prescanDuration).then<void>((_) {
scanSubscription.cancel();
});

return _deviceScanner.currentScan!.future
.then((_) => true)
.catchError((Object _) => false)
.asStream()
.asyncExpand(
(succeeded) {
if (succeeded) {
return _connectIfRecentlyDiscovered(
id, servicesWithCharacteristicsToDiscover, connectionTimeout);
} else {
// When the scan fails 99% of the times it is due to violation of the scan threshold:
// https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
//
// Previously we used "autoconnect" but that gives slow connection times (up to 2 min) on a lot of devices.
return Future<void>.delayed(_delayAfterScanFailure)
.asStream()
.asyncExpand((_) => _connectIfRecentlyDiscovered(
id,
servicesWithCharacteristicsToDiscover,
connectionTimeout,
));
}
},
);
}
return _deviceScanner.currentScan!.future
.then((_) => true)
.catchError((Object _) => false)
.asStream()
.asyncExpand(
(succeeded) {
if (succeeded) {
return _connectIfRecentlyDiscovered(
id, servicesWithCharacteristicsToDiscover, connectionTimeout);
} else {
// When the scan fails 99% of the times it is due to violation of the scan threshold:
// https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
//
// Previously we used "autoconnect" but that gives slow connection times (up to 2 min) on a lot of devices.
return Future<void>.delayed(_delayAfterScanFailure)
.asStream()
.asyncExpand((_) => _connectIfRecentlyDiscovered(
id,
servicesWithCharacteristicsToDiscover,
connectionTimeout,
));
}
},
);
}

Stream<ConnectionStateUpdate> _awaitCurrentScanAndConnect(
Expand Down