8000 Fix Android service-discovery recovery by shroominic · Pull Request #52 · shroominic/OpenGlucose · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ expectations are defined in [docs/compatibility.md](docs/compatibility.md).

## [Unreleased]

### Fixed

- Android AiDEX setup now avoids unnecessary reconnects for existing healthy
bonds. If service discovery times out or the device disconnects, setup waits
for that operation to finish and makes one bounded fresh-connection retry.
The retry preserves the bond and cannot repeat sensor activation. A second
fa 8000 ilure stops automatic session retries.

## [0.1.2] - 2026-08-15

### Changed
Expand Down
13 changes: 9 additions & 4 deletions packages/cgm_aidex/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
of history from becoming a session-wide `StateError`. Deferred work resumes
automatically at the warmup boundary, while genuine post-warmup protocol
failures still become safe session errors.
- Establish the Android OS bond before subscribing to protected
characteristics, refresh GATT before discovery for newly created or existing
bonds, and retain only privacy-safe BLE failure metadata and closed setup
phases. Initialization no longer removes a local bond automatically after an
- Establish the Android OS bond before service discovery, refresh GATT when
setup creates a new bond, and keep an existing healthy bond on its initial
connection. A typed discovery timeout or disconnect now gets one bounded
fresh-GATT retry after the failed operation finishes, without removing the
bond or repeating sensor activation. Exhausting that recovery stops
automatic session retries. Timing profiles can override the recovery close
gap and post-connect settle for deterministic tests.
- Retain only privacy-safe BLE failure metadata and closed setup phases.
Initialization no longer removes a local bond automatically after an
unclassified setup error.

## 1.0.0
Expand Down
150 changes: 116 additions & 34 deletions packages/cgm_aidex/lib/src/aidex_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,10 @@ class AidexSession implements CgmSession {
_setSetupPhase(AidexSetupPhase.bond);
var conn = _connection;
if (conn == null) throw StateError('Disconnected during setup');
if (conn.supportsBondLifecycle) {
await conn.currentBondState();
}
final bondStateBeforeSetup = conn.supportsBondLifecycle
? await conn.currentBondState()
: BleBondState.bonded;
_throwIfDisconnectingDuringSetup();
_setSnapshot(
_snapshot.copyWith(
stage: CgmSyncStage.bonding,
Expand All @@ -403,6 +404,7 @@ class AidexSession implements CgmSession {
final bondStateAfterSetup = conn.supportsBondLifecycle
? await conn.currentBondState()
: BleBondState.bonded;
_throwIfDisconnectingDuringSetup();
if (conn.supportsBondLifecycle &&
bondStateAfterSetup != BleBondState.bonded) {
throw BleFailure(
Expand All @@ -411,23 +413,27 @@ class AidexSession implements CgmSession {
diagnosticCode: 'aidex.bond.not-completed',
);
}
final shouldRefreshBondedGatt =
final establishedBondDuringSetup =
conn.supportsBondLifecycle &&
bondStateBeforeSetup != BleBondState.bonded &&
bondStateAfterSetup == BleBondState.bonded;
_throwIfDisconnectingDuringSetup();
if (shouldRefreshBondedGatt) {
if (establishedBondDuringSetup) {
_setSetupPhase(AidexSetupPhase.reconnect);
await _reconnectGattAfterBond(conn);
await _refreshGattConnection(
conn,
closeGap: _timings.gattGap,
postConnectSettle: Duration.zero,
statusText: 'Refreshing Bluetooth link',
logReason: 'after bond',
);
} else {
_monitorConnectionState();
}
// flutter_blue_plus service discovery can subscribe to the protected
// GAP Service Changed characteristic as an implementation detail. Bond
// before the first discovery so Android can present pairing before any
// protected CCCD write. Refresh every confirmed Android-style bond,
// including a bond from an earlier app run: OEM stacks can preserve a
// stale unauthenticated GATT link even when the bond already exists.
// Discovery therefore runs only on the fresh bonded GATT link.
// Keep bonding before discovery. A bond created on this connection can
// leave OEM Android stacks with stale unauthenticated handles, so give
// only that new bond a fresh GATT connection. A healthy bond from an
// earlier app run stays on its first connection to avoid unnecessary
// disconnect/reconnect churn before service discovery.
_setSnapshot(
_snapshot.copyWith(
stage: CgmSyncStage.bonding,
Expand All @@ -437,7 +443,7 @@ class AidexSession implements CgmSession {
_setSetupPhase(AidexSetupPhase.discovery);
_emitLog(CgmLogLevel.debug, 'Discovering services');
_throwIfDisconnectingDuringSetup();
await _discoverServices();
await _discoverServicesWithRecovery();
_throwIfDisconnectingDuringSetup();
_setSetupPhase(AidexSetupPhase.subscribe);
_emitLog(CgmLogLevel.debug, 'Subscribing to notifications');
Expand Down Expand Up @@ -531,7 +537,10 @@ class AidexSession implements CgmSession {
await _refreshVendorStartTimeInternal();
await _ensureLiveUpdateConfiguration();
final readyMetadata = <String, String>{..._snapshot.metadata}
..remove(aidexSetupPhaseMetadataKey);
..remove(aidexSetupPhaseMetadataKey)
..remove(bleFailureKindMetadataKey)
..remove(bleFailureOperationMetadataKey)
..remove(bleFailureDiagnosticCodeMetadataKey);
_setSnapshot(
_snapshot.copyWith(
stage: CgmSyncStage.ready,
Expand All @@ -547,47 +556,120 @@ class AidexSession implements CgmSession {
}
}

Future<void> _reconnectGattAfterBond(BleConnection bondedConnection) async {
_throwIfDisconnectingDuringSetup();
if (!identical(_connection, bondedConnection)) {
throw StateError('BLE connection changed during bonding');
Future<void> _discoverServicesWithRecovery() async {
final discoveryConnection = _connection;
if (discoveryConnection == null) {
throw StateError('Disconnected before service discovery');
}

try {
await _discoverServices();
return;
} on BleFailure catch (error) {
if (!_isRecoverableDiscoveryFailure(error)) {
rethrow;
}
}

// The first discovery future is terminal before recovery starts. This is
// important for transports that serialize BLE plugin operations.
_throwIfDisconnectingDuringSetup();
_setSetupPhase(AidexSetupPhase.reconnect);
await _refreshGattConnection(
discoveryConnection,
closeGap: _timings.discoveryRecoveryCloseGap,
postConnectSettle: _timings.discoveryRecoveryPostConnectSettle,
statusText: 'Recovering Bluetooth setup',
logReason: 'for service discovery recovery',
);
_throwIfDisconnectingDuringSetup();

_setSnapshot(
_snapshot.copyWith(
stage: CgmSyncStage.bonding,
statusText: 'Refreshing Bluetooth link',
statusText: 'Discovering services',
),
);
_emitLog(CgmLogLevel.debug, 'Refreshing GATT link after bond');
_setSetupPhase(AidexSetupPhase.discovery);
_emitLog(CgmLogLevel.debug, 'Retrying service discovery');
_throwIfDisconnectingDuringSetup();
// One retry only. Any second failure is final and remains in phase P04.
try {
await _discoverServices();
} on BleFailure catch (error, stackTrace) {
if (!_isRecoverableDiscoveryFailure(error)) {
rethrow;
}
Error.throwWithStackTrace(
BleFailure(
kind: BleFailureKind.unexpected,
operation: BleOperation.discoverServices,
diagnosticCode:
'aidex.discovery.${error.kind.name}.recovery-exhausted',
),
stackTrace,
);
}
}

bool _isRecoverableDiscoveryFailure(BleFailure failure) {
return failure.operation == BleOperation.discoverServices &&
(failure.kind == BleFailureKind.operationTimedOut ||
failure.kind == BleFailureKind.deviceDisconnected);
}

// Android vendors can retain stale, unauthenticated GATT handles across a
// newly established bond. Cancel monitoring before this intentional
Future<void> _refreshGattConnection(
BleConnection priorConnection, {
required Duration closeGap,
required Duration postConnectSettle,
required String statusText,
required String logReason,
}) async {
_throwIfDisconnectingDuringSetup();
final activeConnection = _connection;
if (activeConnection != null &&
!identical(activeConnection, priorConnection)) {
throw StateError('BLE connection changed during GATT refresh');
}
_setSnapshot(
_snapshot.copyWith(stage: CgmSyncStage.bonding, statusText: statusText),
);
_emitLog(CgmLogLevel.debug, 'Refreshing GATT link $logReason');

// Android vendors can retain a stale GATT client after a bond transition
// or failed discovery. Cancel monitoring before this intentional
// disconnect so it cannot be reported as a user-visible link loss.
await _connectionStateSubscription?.cancel();
_connectionStateSubscription = null;
_throwIfDisconnectingDuringSetup();
await bondedConnection.disconnect();
await priorConnection.disconnect();
_connection = null;
_connectionState = BleConnectionState.disconnected;

_throwIfDisconnectingDuringSetup();
await Future<void>.delayed(_timings.gattGap);
await Future<void>.delayed(closeGap);
_throwIfDisconnectingDuringSetup();
final refreshedConnection = await _transport.connect(sensor.deviceId);
_connection = refreshedConnection;
_connectionState = BleConnectionState.connected;
_throwIfDisconnectingDuringSetup();

if (refreshedConnection.supportsBondLifecycle &&
await refreshedConnection.currentBondState() != BleBondState.bonded) {
throw BleFailure(
kind: BleFailureKind.bondRejected,
operation: BleOperation.bond,
diagnosticCode: 'aidex.bond.not-persisted',
);
if (refreshedConnection.supportsBondLifecycle) {
final refreshedBondState = await refreshedConnection.currentBondState();
_throwIfDisconnectingDuringSetup();
if (refreshedBondState != BleBondState.bonded) {
throw BleFailure(
kind: BleFailureKind.bondRejected,
operation: BleOperation.bond,
diagnosticCode: 'aidex.bond.not-persisted',
);
}
}

await Future<void>.delayed(postConnectSettle);
_throwIfDisconnectingDuringSetup();
_monitorConnectionState();
_emitLog(CgmLogLevel.debug, 'GATT link refreshed after bond');
_emitLog(CgmLogLevel.debug, 'GATT link refreshed $logReason');
}

void _throwIfDisconnectingDuringSetup() {
Expand Down
4 changes: 4 additions & 0 deletions packages/cgm_aidex/lib/src/aidex_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:pointycastle/export.dart';
class AidexTimingProfile {
const AidexTimingProfile({
this.gattGap = const Duration(milliseconds: 800),
this.discoveryRecoveryCloseGap = const Duration(seconds: 2),
this.discoveryRecoveryPostConnectSettle = const Duration(seconds: 1),
this.postStartSession = const Duration(milliseconds: 350),
this.postSessionStartWrite = const Duration(milliseconds: 250),
this.vendorPairTimeout = const Duration(seconds: 10),
Expand All @@ -17,6 +19,8 @@ class AidexTimingProfile {
});

final Duration gattGap;
final Duration discoveryRecoveryCloseGap;
final Duration discoveryRecoveryPostConnectSettle;
final Duration postStartSession;
final Duration postSessionStartWrite;
final Duration vendorPairTimeout;
Expand Down
Loading
0