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 <noreply@anthropic.com>
pull/741/head
diffwireauto 2 months ago committed by Benjamin Wiechel
parent 68e0d5c58e
commit a04db40b8a

@ -212,21 +212,41 @@ final class FlutterBluePlusLinux extends FlutterBluePlusPlatform {
Stream<BmMtuChangedResponse> get onMtuChanged {
return _client.devicesChanged
.switchMap((devices) {
final streams = <Stream<BlueZDevice>>[];
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 <String>[]).switchMap((_) {
final streams = <Stream<BlueZDevice>>[];
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;

Loading…
Cancel
Save

Powered by TurnKey Linux.