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
parent
b3a194a356
commit
4e46ec05ec
@ -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';
|
||||
}
|
||||
@ -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…
Reference in new issue