From 4df0c477a3aa6ec2ad86bf5240b1b6305fd1578f Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Mon, 5 Oct 2026 14:24:38 -0400 Subject: [PATCH] fix(#807): honest systemDevices merge + Windows live-scan filter #570 option A (owner-approved). The systemDevices merge is the #49 coexistence door (attach to a radio another app connected), so it keeps every OS-connected device rather than dropping by name, which would hide a never-handshaked or renamed radio. Changes: - stop fabricating serviceUuids: [NUS] on synthesized entries (it lied and defeated downstream service filtering); rssi 0 is now documented as an OS-connected sentinel, exposed via isSystemConnected() for the UI (#809) to badge instead of showing a signal bar. - track system-connected ids; honor a _suppressedSystemDeviceIds set that the negative-memory store (#808) will populate. - Windows-only live-scan name-prefix filter: the vendored WinRT watcher ignores withServices and forwards every advertiser, so non-radios are filtered in the live path on Windows (Android/iOS already OS-filter; web uses the chooser). The plugin-level service filter was reverted (Gemini review: rests on an unverified adv assumption). - matchesDeviceNamePrefix is now case-insensitive (review) and gained the Offband- prefix (its own radios must not be filtered). Parent #806. Co-Authored-By: Claude Opus 4.8 --- lib/connector/meshcore_connector.dart | 80 ++++++++++++++++++++- lib/connector/meshcore_uuids.dart | 10 +++ test/connector/device_name_prefix_test.dart | 67 +++++++++++++++++ 3 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 test/connector/device_name_prefix_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 58219ea4..41fdb28a 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -419,6 +419,28 @@ class MeshCoreConnector extends ChangeNotifier { final List _scanResults = []; final List _systemScanResults = []; + + // #570/#806: remoteIds surfaced via the systemDevices merge (OS-connected, + // no live advertisement, so rssi is a sentinel not a signal). The scanner UI + // (#809) groups and badges these instead of showing a signal bar. + final Set _systemConnectedIds = {}; + + // #808: remoteIds a user-initiated attach proved are not MeshCore radios + // (NUS discovery failed). Suppressed from the systemDevices merge. Loaded + // from and persisted by the negative-memory store. + final Set _suppressedSystemDeviceIds = {}; + + /// Whether [remoteId] in the scan list came from the systemDevices merge + /// (OS-connected, not advertising). Such entries have no real rssi and must + /// be shown as connected rather than with a signal bar (#570/#809). + bool isSystemConnected(String remoteId) => + _systemConnectedIds.contains(remoteId); + + /// Whether a scan entry looks like a radio by its advertised/cached name. + /// Used by the UI to split verified radios from other OS-connected devices + /// (#809); the systemDevices merge itself keeps both (preserves #49). + bool looksLikeRadioName(String name) => + MeshCoreUuids.matchesDeviceNamePrefix(name); final List _contacts = []; bool _contactsFullLogged = false; final List _discoveredContacts = []; @@ -3685,9 +3707,18 @@ class MeshCoreConnector extends ChangeNotifier { } _scanSubscription = FlutterBluePlus.scanResults.listen((results) { + // #570: the Windows plugin's scan ignores the withServices filter, so + // it forwards every advertiser. Android/iOS filter at the OS level from + // the same withServices argument (never narrow those by name, or a radio + // with a custom name advertising the service would be hidden), and web + // gates through the browser chooser. So the name filter applies to + // Windows only. + final filtered = PlatformInfo.isWindows + ? results.where(_looksLikeRadio) + : results; _scanResults ..clear() - ..addAll(results); + ..addAll(filtered); _mergeSystemScanResults(); notifyListeners(); }); @@ -3709,15 +3740,54 @@ class MeshCoreConnector extends ChangeNotifier { await stopScan(); } + // #570: a scan result is a radio if its advertised or cached name matches a + // known radio prefix, or if it is the device this app currently holds. The + // live scan has no service filter on Windows (the watcher forwards every + // advertiser), so this is where non-radio devices are kept out of the list. + // The connectable advertisement of a MeshCore-type radio carries its name, + // which is the same signal the device tile already displays. + bool _looksLikeRadio(ScanResult result) { + if (result.device.remoteId.str == _deviceId) return true; + final adv = result.advertisementData.advName; + final name = adv.isNotEmpty ? adv : result.device.platformName; + return MeshCoreUuids.matchesDeviceNamePrefix(name); + } + Future _loadSystemDevicesForScan() async { try { final systemDevices = await FlutterBluePlus.systemDevices([ Guid(MeshCoreUuids.service), ]); + // This door is the #49 coexistence feature: surface a radio another app + // connected (so it stopped advertising) that this app may never have + // handshaked. So we must NOT drop entries by name (#570 / #806 option A): + // a legitimate never-handshaked or renamed radio has no prefix to match. + // Instead keep every entry, tag which ids came from here (no real rssi, + // OS-connected) so the UI (#809) can group and badge them honestly, and + // let the negative-memory store (#808) suppress ones a user-initiated + // attach has already proven are not radios. + // + // The withServices argument is honored only on iOS (FBP's own doc); + // Android and the vendored Windows plugin ignore it and return every + // GATT-connected device. systemDevices carries no service info on any + // native platform, so we do NOT fabricate a serviceUuids: [NUS] stamp + // (that lied to the user and defeated downstream service filtering) and + // we do NOT present rssi as a real value. + final visible = systemDevices + .where((d) => !_suppressedSystemDeviceIds.contains(d.remoteId.str)) + .toList(); + _systemConnectedIds + ..clear() + ..addAll(visible.map((d) => d.remoteId.str)); + _appDebugLogService?.info( + 'systemDevices: ${visible.length} OS-connected device(s) ' + '(${systemDevices.length - visible.length} suppressed) (#570)', + tag: 'BLE Scan', + ); _systemScanResults ..clear() ..addAll( - systemDevices.map( + visible.map( (device) => ScanResult( device: device, advertisementData: AdvertisementData( @@ -3727,8 +3797,12 @@ class MeshCoreConnector extends ChangeNotifier { connectable: true, manufacturerData: const >{}, serviceData: const >{}, - serviceUuids: [Guid(MeshCoreUuids.service)], + // No fabricated service list (#570): these are not advertising. + serviceUuids: const [], ), + // rssi 0 is a sentinel meaning "OS-connected, no advertisement"; + // the tile must render it as a connected badge, never a signal + // bar (#809). isSystemConnected() distinguishes it. rssi: 0, timeStamp: DateTime.now(), ), diff --git a/lib/connector/meshcore_uuids.dart b/lib/connector/meshcore_uuids.dart index 084cc424..11011a78 100644 --- a/lib/connector/meshcore_uuids.dart +++ b/lib/connector/meshcore_uuids.dart @@ -5,6 +5,7 @@ class MeshCoreUuids { static const List deviceNamePrefixes = [ "MeshCore-", + "Offband-", "Whisper-", "WisCore-", "Seeed", @@ -13,4 +14,13 @@ class MeshCoreUuids { "LowMesh_MC_", "NRF52", ]; + + /// Whether [name] looks like a radio by its advertised/cached name (#570). + /// Case-insensitive: BLE device names carry no guaranteed casing, so a + /// firmware variant advertising `meshcore-...` or `seeed...` must still + /// match. + static bool matchesDeviceNamePrefix(String name) { + final lower = name.toLowerCase(); + return deviceNamePrefixes.any((p) => lower.startsWith(p.toLowerCase())); + } } diff --git a/test/connector/device_name_prefix_test.dart b/test/connector/device_name_prefix_test.dart new file mode 100644 index 00000000..6ec169a1 --- /dev/null +++ b/test/connector/device_name_prefix_test.dart @@ -0,0 +1,67 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_uuids.dart'; + +/// #570: the scanner filters non-radio BLE devices (paired watches, PCs) out +/// of the list by name prefix, because systemDevices and the Windows scan +/// carry no usable service filter. These cases pin the prefix set and the +/// matcher's exact-prefix (not substring) behavior. +void main() { + group('MeshCoreUuids.matchesDeviceNamePrefix (#570)', () { + test('accepts known radio name prefixes', () { + const radios = [ + 'MeshCore-WSMJ898', + 'Offband-MC-Strycher-R1W', + 'Whisper-abcd', + 'WisCore-1234', + 'Seeed XIAO', + 'Lilygo T-Beam', + 'HT-n5262', + 'LowMesh_MC_01', + 'NRF52-DK', + ]; + for (final name in radios) { + expect( + MeshCoreUuids.matchesDeviceNamePrefix(name), + isTrue, + reason: '$name should be recognized as a radio', + ); + } + }); + + test('rejects non-radio device names', () { + const notRadios = [ + 'Galaxy Watch6', + "Ben's MacBook Pro", + 'Pixel Buds', + 'Unknown Device', + '', // empty cached name + 'nimble', // stale cached name seen in #189 + ]; + for (final name in notRadios) { + expect( + MeshCoreUuids.matchesDeviceNamePrefix(name), + isFalse, + reason: '$name should not be recognized as a radio', + ); + } + }); + + test('matches on prefix, not substring', () { + // A name that merely contains a prefix mid-string is not a radio. + expect( + MeshCoreUuids.matchesDeviceNamePrefix('My MeshCore-ish toy'), + isFalse, + ); + }); + + test('is case-insensitive', () { + expect(MeshCoreUuids.matchesDeviceNamePrefix('meshcore-abcd'), isTrue); + expect(MeshCoreUuids.matchesDeviceNamePrefix('OFFBAND-1234'), isTrue); + expect(MeshCoreUuids.matchesDeviceNamePrefix('seeed xiao'), isTrue); + }); + + test('Offband- is included (its own radios must not be filtered out)', () { + expect(MeshCoreUuids.deviceNamePrefixes, contains('Offband-')); + }); + }); +}