fix(#1186): log why the OS dropped a BLE link, and the app lifecycle

A user's iPhone dropped its BLE link 44 times in 3 hours, and no log line
said why. Every drop came from the OS through the connectionState
listener, which ended the connection without recording a reason.

Each OS-reported drop now writes one line:
- the platform's disconnect reason (the CoreBluetooth code on iOS and
  macOS, the HCI status on Android),
- the seconds since the last frame from the radio,
- the app lifecycle state.

Windows, Linux and web report no reason, and the line says "not
available on <platform>" (#1187 tracks getting a real one there). The
reason is read one microtask after the event, because flutter_blue_plus
records it in its own listener on the same event.

main.dart logs every app lifecycle change and passes the state to the
connector, so a drop can be lined up against iOS backgrounding the app.

Diagnostics only: the disconnect and reconnect flow is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1186-ble-drop-reason
diffwireauto 3 days ago
parent b3a194a356
commit 4e46ec05ec

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math' as math;
import 'dart:ui' show AppLifecycleState;
import 'package:crypto/crypto.dart' as crypto;
import 'package:pointycastle/export.dart';
@ -17,6 +18,7 @@ import '../models/message.dart';
import '../models/offband_gps_status.dart';
import '../models/path_selection.dart';
import '../models/translation_support.dart';
import '../helpers/ble_drop_reason.dart';
import '../helpers/channel_restore_plan.dart';
import '../helpers/channel_send_timestamp.dart';
import '../helpers/pending_reactions.dart';
@ -4115,6 +4117,26 @@ class MeshCoreConnector extends ChangeNotifier {
_throwIfSuperseded(generation);
_connectionSubscription = device.connectionState.listen((state) {
if (state == BluetoothConnectionState.disconnected && isConnected) {
final sinceLastRx = DateTime.now().difference(_lastRxTime);
final lifecycle = appLifecycleState;
// flutter_blue_plus records the reason in its own listener on the
// same event; reading after this event is delivered avoids
// depending on which listener runs first (#1186).
scheduleMicrotask(() {
_appDebugLogService?.warn(
describeBleDrop(
reason: device.disconnectReason,
platform: _blePlatformName,
platformReportsReason:
PlatformInfo.isIOS ||
PlatformInfo.isMacOS ||
PlatformInfo.isAndroid,
sinceLastRx: sinceLastRx,
lifecycle: lifecycle,
),
tag: 'Connection',
);
});
_handleDisconnection();
}
});
@ -5021,6 +5043,20 @@ class MeshCoreConnector extends ChangeNotifier {
if (generation != _connectGeneration) throw const _ConnectSuperseded();
}
/// The app's lifecycle state, set from main.dart, so a BLE drop's log line
/// can say whether the app was in the background (#1186).
AppLifecycleState? appLifecycleState;
String get _blePlatformName {
if (PlatformInfo.isWeb) return 'web';
if (PlatformInfo.isIOS) return 'iOS';
if (PlatformInfo.isMacOS) return 'macOS';
if (PlatformInfo.isAndroid) return 'Android';
if (PlatformInfo.isWindows) return 'Windows';
if (PlatformInfo.isLinux) return 'Linux';
return 'this platform';
}
void _scheduleReconnect() {
if (!_shouldAutoReconnect) return;
if (_reconnectTimer?.isActive == true) return;

@ -0,0 +1,33 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
/// The log line written when the OS reports a BLE link drop (#1186), so a user
/// log says why the link ended instead of only that it did.
///
/// iOS, macOS and Android report a reason code. Windows, Linux and web report
/// none (#1187), and the line says so rather than calling it unknown.
String describeBleDrop({
required DisconnectReason? reason,
required String platform,
required bool platformReportsReason,
required Duration sinceLastRx,
required AppLifecycleState? lifecycle,
}) {
final code = reason?.code;
final String why;
if (!platformReportsReason) {
why = 'reason not available on $platform';
} else if (code == null) {
why = '$platform gave no reason code';
} else {
final description = reason!.description;
why =
'${reason.platform.name}-code $code'
'${description == null || description.isEmpty ? '' : ': $description'}';
}
final app = lifecycle == null
? 'app state not reported'
: 'app ${lifecycle.name}';
return 'BLE link dropped by $platform ($why); '
'last frame from the radio ${sinceLastRx.inSeconds}s ago; $app';
}

@ -196,6 +196,18 @@ void main() async {
),
);
// On iOS a BLE drop often follows the app going to the background (#1186).
// Logging each change lets a drop be lined up against it. The listener
// registers itself with WidgetsBinding, which keeps it alive for the life
// of the app; it is never disposed.
connector.appLifecycleState = WidgetsBinding.instance.lifecycleState;
AppLifecycleListener(
onStateChange: (state) {
connector.appLifecycleState = state;
appLogger.info('App lifecycle: ${state.name}', tag: 'Lifecycle');
},
);
// Report why the previous process died, if it did (#906). Scheduled after the
// first frame and deliberately not awaited: #334 is the leading hypothesis for
// the ANRs this exists to diagnose, so the collector must not itself add work

@ -0,0 +1,86 @@
// #1186: a BLE drop must log why the link ended. iOS and Android give a reason
// code; Windows, Linux and web give none, and the line says so (#1187).
import 'package:flutter/widgets.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/ble_drop_reason.dart';
void main() {
String line({
DisconnectReason? reason,
String platform = 'iOS',
bool reports = true,
int secs = 0,
AppLifecycleState? lifecycle = AppLifecycleState.resumed,
}) => describeBleDrop(
reason: reason,
platform: platform,
platformReportsReason: reports,
sinceLastRx: Duration(seconds: secs),
lifecycle: lifecycle,
);
test('iOS: the CoreBluetooth code and text', () {
final l = line(
reason: DisconnectReason(
ErrorPlatform.apple,
15,
'Failed to encrypt the connection',
),
);
expect(l, contains('apple-code 15: Failed to encrypt the connection'));
expect(l, contains('dropped by iOS'));
});
test('Android: the HCI status code and name', () {
final l = line(
platform: 'Android',
reason: DisconnectReason(
ErrorPlatform.android,
8,
'LINK_SUPERVISION_TIMEOUT',
),
);
expect(l, contains('android-code 8: LINK_SUPERVISION_TIMEOUT'));
});
test('a code with no text still shows the code', () {
final l = line(reason: DisconnectReason(ErrorPlatform.apple, 7, null));
expect(l, contains('apple-code 7)'));
});
test('a code with empty text shows only the code', () {
final l = line(reason: DisconnectReason(ErrorPlatform.apple, 7, ''));
expect(l, contains('apple-code 7)'));
expect(l, isNot(contains('7: ')));
});
test('a reporting platform that gave no code says so', () {
final l = line(reason: DisconnectReason(ErrorPlatform.apple, null, null));
expect(l, contains('iOS gave no reason code'));
});
for (final platform in ['Windows', 'Linux', 'web']) {
test('$platform: not available, never "unknown"', () {
final l = line(
platform: platform,
reports: false,
reason: DisconnectReason(ErrorPlatform.fbp, null, null),
);
expect(l, contains('reason not available on $platform'));
expect(l.toLowerCase(), isNot(contains('unknown')));
});
}
test('time since the last frame and the app state', () {
final l = line(secs: 261, lifecycle: AppLifecycleState.paused);
expect(l, contains('last frame from the radio 261s ago'));
expect(l, contains('app paused'));
});
test('no lifecycle state reported', () {
final l = line(lifecycle: null);
expect(l, contains('app state not reported'));
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.