From a04db40b8afac5b762021e28180008d553d676ce Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Thu, 1 Oct 2026 01:03:30 -0400 Subject: [PATCH] fix(#717): apply pre-PR review, rebuild MTU subscriptions once services resolve Gemini gate found a real defect that would have shipped: the Linux MTU stream would have attached to nothing and reported an MTU for a device never. `devicesChanged` fires only on deviceAdded / deviceRemoved. At the moment a device is added BlueZ has not finished service discovery, so `gattServices` is still empty; this plugin waits on `while (!device.servicesResolved)` elsewhere for exactly that reason. The original patch built its per-characteristic subscriptions inside that one event, found no characteristics, and had nothing to re-trigger it when discovery later completed. Verified in the vendored source, not taken on the reviewer's word. Now nested: for each device, rebuild the characteristic subscriptions on every `device.propertiesChanged`, which includes ServicesResolved flipping true. Same shape the plugin's own `_initFlutterBluePlus` already uses for its logging, so this is the file's existing idiom rather than a new one. Also emits once per rebuild (`startWith(device)`), so an MTU already negotiated before we were listening is reported rather than waiting on a change that may never arrive. Safe when unknown: 0 is filtered downstream. Two other findings reviewed and accepted as clean, recorded so they are not re-litigated: "max across characteristics" is right because the ATT MTU is per-connection, not per-characteristic; and the web exemption is sound because the browser owns fragmentation and 172 is far inside the spec's 512-byte writeValue limit. analyze clean, format clean, 981 tests pass. Co-Authored-By: Claude Opus 5 --- .../lib/flutter_blue_plus_linux.dart | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/packages/flutter_blue_plus_linux/lib/flutter_blue_plus_linux.dart b/packages/flutter_blue_plus_linux/lib/flutter_blue_plus_linux.dart index 76012985..b25a399b 100644 --- a/packages/flutter_blue_plus_linux/lib/flutter_blue_plus_linux.dart +++ b/packages/flutter_blue_plus_linux/lib/flutter_blue_plus_linux.dart @@ -212,21 +212,41 @@ final class FlutterBluePlusLinux extends FlutterBluePlusPlatform { Stream get onMtuChanged { return _client.devicesChanged .switchMap((devices) { - final streams = >[]; - - for (final device in devices) { - for (final service in device.gattServices) { - for (final characteristic in service.characteristics) { - streams.add( - characteristic.propertiesChanged - .where((properties) => properties.contains('MTU')) - .map((properties) => device), - ); - } - } - } - - return MergeStream(streams); + return MergeStream( + devices.map((device) { + // Rebuild the per-characteristic subscriptions whenever the + // device's own properties change, NOT just when the device list + // does. devicesChanged only fires on deviceAdded/deviceRemoved, + // and at the moment a device is added BlueZ has not finished + // service discovery, so gattServices is still empty: this plugin + // waits on `while (!device.servicesResolved)` elsewhere for + // exactly that reason. Subscribing only on the device-list event + // would therefore attach to nothing and miss every later MTU for + // that device, permanently. ServicesResolved flipping true is a + // device property change, so this re-runs once characteristics + // actually exist. Same nested shape _initFlutterBluePlus uses. + return device.propertiesChanged + .startWith(const []).switchMap((_) { + final streams = >[]; + + for (final service in device.gattServices) { + for (final characteristic in service.characteristics) { + streams.add( + characteristic.propertiesChanged + .where((properties) => properties.contains('MTU')) + .map((properties) => device), + ); + } + } + + // Emit once on every rebuild so an MTU already negotiated + // before we were listening is reported, instead of waiting + // for a change that may never come. Harmless when unknown: + // the 0 is filtered out downstream. + return MergeStream(streams).startWith(device); + }); + }), + ); }) .map((device) { var best = 0;