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 <noreply@anthropic.com>
pull/1355/head
diffwireauto 2 months ago
parent ce8da46764
commit 4df0c477a3

@ -419,6 +419,28 @@ class MeshCoreConnector extends ChangeNotifier {
final List<ScanResult> _scanResults = [];
final List<ScanResult> _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<String> _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<String> _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<Contact> _contacts = [];
bool _contactsFullLogged = false;
final List<Contact> _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<void> _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 <int, List<int>>{},
serviceData: const <Guid, List<int>>{},
serviceUuids: <Guid>[Guid(MeshCoreUuids.service)],
// No fabricated service list (#570): these are not advertising.
serviceUuids: const <Guid>[],
),
// 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(),
),

@ -5,6 +5,7 @@ class MeshCoreUuids {
static const List<String> 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()));
}
}

@ -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-'));
});
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.