Merge pull request #1355 from OffbandMesh/fix/807-scanner-hygiene-dev
fix(#806): scanner device-list hygiene (#807, #808, #809)pull/1359/head
commit
3c2ba04b7b
@ -0,0 +1,50 @@
|
|||||||
|
import 'prefs_manager.dart';
|
||||||
|
|
||||||
|
/// Remembers BLE devices a user-initiated attach proved are NOT MeshCore
|
||||||
|
/// radios, so the scanner stops surfacing them (#570 / #808).
|
||||||
|
///
|
||||||
|
/// The `systemDevices` merge is the #49 coexistence door: it lists every
|
||||||
|
/// device the OS has a GATT connection to, which includes a paired watch or
|
||||||
|
/// PC. We cannot drop those by name without also hiding a legitimate radio
|
||||||
|
/// another app connected (which may carry no recognizable name). Instead, once
|
||||||
|
/// the user taps such a device to attach and NUS service discovery fails, we
|
||||||
|
/// know for certain it is not a radio and hide it from then on.
|
||||||
|
///
|
||||||
|
/// Global, NOT scoped per connected radio: "this watch is not a radio" is true
|
||||||
|
/// regardless of which radio the app later connects to. Learned ONLY from
|
||||||
|
/// user-initiated attach failures, never from passive scans.
|
||||||
|
class NonRadioDeviceStore {
|
||||||
|
static const String _key = 'non_radio_device_ids';
|
||||||
|
|
||||||
|
/// The remembered non-radio remote ids. Empty when nothing learned yet or
|
||||||
|
/// when SharedPreferences is unavailable.
|
||||||
|
static Set<String> load() {
|
||||||
|
try {
|
||||||
|
return PrefsManager.instance.getStringList(_key)?.toSet() ?? <String>{};
|
||||||
|
} catch (_) {
|
||||||
|
return <String>{};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remember [remoteId] as not-a-radio. Idempotent. Returns the updated set.
|
||||||
|
static Set<String> add(String remoteId) {
|
||||||
|
final ids = load()..add(remoteId);
|
||||||
|
try {
|
||||||
|
PrefsManager.instance.setStringList(_key, ids.toList());
|
||||||
|
} catch (_) {
|
||||||
|
// Best effort: suppression still holds for this session via the
|
||||||
|
// in-memory set the connector keeps; it just won't survive a restart.
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Forget [remoteId] (e.g. if it later turns out to be a radio). Returns the
|
||||||
|
/// updated set.
|
||||||
|
static Set<String> remove(String remoteId) {
|
||||||
|
final ids = load()..remove(remoteId);
|
||||||
|
try {
|
||||||
|
PrefsManager.instance.setStringList(_key, ids.toList());
|
||||||
|
} catch (_) {}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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-'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:meshcore_open/connector/meshcore_uuids.dart';
|
||||||
|
|
||||||
|
/// #807: the Windows live scan filters advertisers by these name prefixes,
|
||||||
|
/// because the WinRT watcher ignores the service filter. Offband radios
|
||||||
|
/// advertise as `Offband-...`, so without that prefix the filter would hide
|
||||||
|
/// our own radios. Uses only the prefix list, which predates #807.
|
||||||
|
void main() {
|
||||||
|
test('Offband radios are recognized by name prefix (#807)', () {
|
||||||
|
const offbandRadio = 'Offband-MC-Strycher-R1W';
|
||||||
|
expect(
|
||||||
|
MeshCoreUuids.deviceNamePrefixes.any(offbandRadio.startsWith),
|
||||||
|
isTrue,
|
||||||
|
reason: 'an Offband radio must not be filtered out of the scan',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in new issue