diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index a778f01f..65852e81 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -59,6 +59,7 @@ import '../storage/contact_discovery_store.dart'; import '../storage/contact_settings_store.dart'; import '../storage/contact_store.dart'; import '../storage/message_store.dart'; +import '../storage/non_radio_device_store.dart'; import '../storage/radio_data_wipe.dart'; import '../storage/unread_store.dart'; import '../utils/app_logger.dart'; @@ -427,6 +428,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 = []; @@ -3646,6 +3669,11 @@ class MeshCoreConnector extends ChangeNotifier { _scanResults.clear(); _systemScanResults.clear(); + // #808: refresh the learned not-a-radio set so the systemDevices merge + // suppresses them this scan. + _suppressedSystemDeviceIds + ..clear() + ..addAll(NonRadioDeviceStore.load()); _setState(MeshCoreConnectionState.scanning); // Ensure any previous scan is fully stopped. Guard with isScanningNow to @@ -3693,9 +3721,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(); }); @@ -3717,15 +3754,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( @@ -3735,8 +3811,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(), ), @@ -4068,6 +4148,9 @@ class MeshCoreConnector extends ChangeNotifier { BluetoothDevice device, { String? displayName, Future Function()? linuxPairingPinProvider, + // #808: a user tapping a device to attach (auto false) is the only signal + // trusted to learn "not a radio". Auto-reconnect never permanently hides + // the user's own radio over a transient NUS miss. required bool auto, }) async { final attemptLabel = displayName ?? device.remoteId.toString(); @@ -4381,6 +4464,20 @@ class MeshCoreConnector extends ChangeNotifier { } if (uartService == null) { + // #808: the user chose this device and it has no MeshCore UART + // service, so it is not a radio. Remember it so the systemDevices + // merge stops surfacing it. Only on a user-initiated attach. + if (!auto) { + final id = device.remoteId.str; + _suppressedSystemDeviceIds + ..clear() + ..addAll(NonRadioDeviceStore.add(id)); + _appDebugLogService?.info( + 'Remembered $id as not-a-radio (NUS discovery failed on attach) ' + '(#808)', + tag: 'BLE Connect', + ); + } throw Exception("MeshCore UART service not found"); } 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/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 31fd24fb..1589be95 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -146,6 +146,23 @@ }, "scanner_searchingDevices": "Searching for MeshCore devices...", "scanner_tapToScan": "Tap Scan to find MeshCore devices", + "scanner_connectedBadge": "Connected", + "@scanner_connectedBadge": { + "description": "Leading badge on a scanner tile for a device the OS has a BLE connection to but that is not advertising, shown instead of a signal strength bar (#570)." + }, + "scanner_connectedToPhone": "Connected to this phone ({count})", + "@scanner_connectedToPhone": { + "description": "Collapsed group header for OS-connected BLE devices that do not look like radios (#570).", + "placeholders": { + "count": { + "type": "int" + } + } + }, + "scanner_connectedToPhoneHint": "Devices already connected to this phone. Tap one only if it is a MeshCore radio.", + "@scanner_connectedToPhoneHint": { + "description": "Sub-label under the connected-to-phone group explaining why these appear (#570)." + }, "scanner_connectionFailed": "Connection failed: {error}", "@scanner_connectionFailed": { "placeholders": { diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index 1faf0b65..0dcb5951 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -634,6 +634,24 @@ abstract class AppLocalizations { /// **'Tap Scan to find MeshCore devices'** String get scanner_tapToScan; + /// Leading badge on a scanner tile for a device the OS has a BLE connection to but that is not advertising, shown instead of a signal strength bar (#570). + /// + /// In en, this message translates to: + /// **'Connected'** + String get scanner_connectedBadge; + + /// Collapsed group header for OS-connected BLE devices that do not look like radios (#570). + /// + /// In en, this message translates to: + /// **'Connected to this phone ({count})'** + String scanner_connectedToPhone(int count); + + /// Sub-label under the connected-to-phone group explaining why these appear (#570). + /// + /// In en, this message translates to: + /// **'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'** + String get scanner_connectedToPhoneHint; + /// No description provided for @scanner_connectionFailed. /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index a976e818..a88a48f7 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -289,6 +289,18 @@ class AppLocalizationsBg extends AppLocalizations { String get scanner_tapToScan => 'Натиснете Сканиране, за да намерите устройства MeshCore.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Връзката не успя: $error'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index d9e90a77..3f059444 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -292,6 +292,18 @@ class AppLocalizationsDe extends AppLocalizations { String get scanner_tapToScan => 'Tippen Sie auf Scan, um MeshCore-Geräte zu finden.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Verbindungsfehler: $error'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index b0bc5ec1..c55ccaf3 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -286,6 +286,18 @@ class AppLocalizationsEn extends AppLocalizations { @override String get scanner_tapToScan => 'Tap Scan to find MeshCore devices'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Connection failed: $error'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index bcbccbe0..8401054e 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -290,6 +290,18 @@ class AppLocalizationsEs extends AppLocalizations { String get scanner_tapToScan => 'Toca Escanear para encontrar dispositivos MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Error de conexión: $error'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index f8284154..b107ab91 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -292,6 +292,18 @@ class AppLocalizationsFr extends AppLocalizations { String get scanner_tapToScan => 'Appuyez sur Scanner pour trouver les appareils MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Échec de la connexion : $error'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index 1157a2d5..a46e038b 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -289,6 +289,18 @@ class AppLocalizationsHu extends AppLocalizations { String get scanner_tapToScan => 'A Tap Scan funkció segítségével kereshet MeshCore eszközöket.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Kapcsolódás sikertelen: $error'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index 5d162dea..8153226a 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -292,6 +292,18 @@ class AppLocalizationsIt extends AppLocalizations { String get scanner_tapToScan => 'Tocca Scansiona per trovare i dispositivi MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Connessione fallita: $error'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 26d0b7c9..11862a03 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -280,6 +280,18 @@ class AppLocalizationsJa extends AppLocalizations { @override String get scanner_tapToScan => 'MeshCore デバイスを検索するには、「スキャン」ボタンをタップしてください。'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return '接続に失敗しました:$error'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index da32abef..ac3c2e79 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -279,6 +279,18 @@ class AppLocalizationsKo extends AppLocalizations { @override String get scanner_tapToScan => 'MeshCore 장치를 찾기 위해 스캔 버튼을 누르세요.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return '연결 실패: $error'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index 2e313152..97ab9cab 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -288,6 +288,18 @@ class AppLocalizationsNl extends AppLocalizations { @override String get scanner_tapToScan => 'Tik Scan om MeshCore apparaten te vinden'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Verbinding mislukt: $error'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 84cfc0a5..7a31c533 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -293,6 +293,18 @@ class AppLocalizationsPl extends AppLocalizations { String get scanner_tapToScan => 'Naciśnij Skan, aby znaleźć urządzenia MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Połączenie nieudane: $error'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index 949f1a0c..4e14db38 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -291,6 +291,18 @@ class AppLocalizationsPt extends AppLocalizations { String get scanner_tapToScan => 'Toque em \"Escanear\" para encontrar dispositivos MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Falha na conexão: $error'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index 49f82aa6..dbae3e6b 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -291,6 +291,18 @@ class AppLocalizationsRu extends AppLocalizations { @override String get scanner_tapToScan => 'Нажмите для поиска MeshCore устройств'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Подключение не удалось: $error'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 9350ee1e..9f2f20eb 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -290,6 +290,18 @@ class AppLocalizationsSk extends AppLocalizations { String get scanner_tapToScan => 'Stlač skenovanie na nájdenie zariadení MeshCore.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Pripojenie zlyhalo: $error'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index 35734532..39804824 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -288,6 +288,18 @@ class AppLocalizationsSl extends AppLocalizations { String get scanner_tapToScan => 'Nagneš na skeniranje za najdene naprave MeshCore.'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Pošlo je z povezavo: $error'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 57a42e1b..958b723f 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -287,6 +287,18 @@ class AppLocalizationsSv extends AppLocalizations { @override String get scanner_tapToScan => 'Tryck Skanna för att hitta MeshCore-enheter'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Anslutning misslyckades: $error'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index 3f084d93..38de408c 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -290,6 +290,18 @@ class AppLocalizationsUk extends AppLocalizations { String get scanner_tapToScan => 'Натисніть «Сканувати», щоб знайти пристрої MeshCore'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return 'Помилка підключення: $error'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 8ecfac4d..8b0620e1 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -277,6 +277,18 @@ class AppLocalizationsZh extends AppLocalizations { @override String get scanner_tapToScan => '点击“扫描”按钮以查找 MeshCore 设备。'; + @override + String get scanner_connectedBadge => 'Connected'; + + @override + String scanner_connectedToPhone(int count) { + return 'Connected to this phone ($count)'; + } + + @override + String get scanner_connectedToPhoneHint => + 'Devices already connected to this phone. Tap one only if it is a MeshCore radio.'; + @override String scanner_connectionFailed(String error) { return '连接失败:$error'; diff --git a/lib/screens/scanner_screen.dart b/lib/screens/scanner_screen.dart index 2500d7c7..6efe90e1 100644 --- a/lib/screens/scanner_screen.dart +++ b/lib/screens/scanner_screen.dart @@ -356,17 +356,56 @@ class _ScannerScreenState extends State { ); } - return ListView.separated( + // #570/#809: split the list. A live advertiser, or an OS-connected device + // whose name looks like a radio, lists normally. Other OS-connected + // devices (a paired watch/PC the systemDevices merge surfaces, kept so a + // never-handshaked radio is never hidden, #49) go in a collapsed + // "Connected to this phone" group with an honest badge, still attachable. + final main = []; + final otherConnected = []; + for (final result in connector.scanResults) { + final id = result.device.remoteId.str; + final adv = result.advertisementData.advName; + final name = adv.isNotEmpty ? adv : result.device.platformName; + if (connector.isSystemConnected(id) && + !connector.looksLikeRadioName(name)) { + otherConnected.add(result); + } else { + main.add(result); + } + } + + return ListView( padding: const EdgeInsets.all(8), - itemCount: connector.scanResults.length, - separatorBuilder: (context, index) => const Divider(), - itemBuilder: (context, index) { - final result = connector.scanResults[index]; - return DeviceTile( - scanResult: result, - onTap: () => _connectToDevice(context, connector, result), - ); - }, + children: [ + for (final result in main) ...[ + DeviceTile( + scanResult: result, + systemConnected: connector.isSystemConnected( + result.device.remoteId.str, + ), + onTap: () => _connectToDevice(context, connector, result), + ), + const Divider(), + ], + if (otherConnected.isNotEmpty) + ExpansionTile( + leading: const Icon(Icons.devices_other), + title: Text( + context.l10n.scanner_connectedToPhone(otherConnected.length), + ), + subtitle: Text(context.l10n.scanner_connectedToPhoneHint), + initiallyExpanded: false, + children: [ + for (final result in otherConnected) + DeviceTile( + scanResult: result, + systemConnected: true, + onTap: () => _connectToDevice(context, connector, result), + ), + ], + ), + ], ); } diff --git a/lib/storage/non_radio_device_store.dart b/lib/storage/non_radio_device_store.dart new file mode 100644 index 00000000..2558fa17 --- /dev/null +++ b/lib/storage/non_radio_device_store.dart @@ -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 load() { + try { + return PrefsManager.instance.getStringList(_key)?.toSet() ?? {}; + } catch (_) { + return {}; + } + } + + /// Remember [remoteId] as not-a-radio. Idempotent. Returns the updated set. + static Set 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 remove(String remoteId) { + final ids = load()..remove(remoteId); + try { + PrefsManager.instance.setStringList(_key, ids.toList()); + } catch (_) {} + return ids; + } +} diff --git a/lib/storage/scoping_inventory.dart b/lib/storage/scoping_inventory.dart index 3b49cfe7..13f5d4f3 100644 --- a/lib/storage/scoping_inventory.dart +++ b/lib/storage/scoping_inventory.dart @@ -266,6 +266,16 @@ const List storageSurfaces = [ 'connected, and per-radio caching would multiply the download', owner: 'lib/services/map_tile_cache_service.dart', ), + StorageSurface( + 'non_radio_device_ids', + verdict: ScopingVerdict.globalOnPurpose, + reason: + 'Bluetooth ids of devices this phone proved are not radios (no ' + 'MeshCore UART service on a user-initiated attach, #808). It is a ' + 'fact about the device, learned before any radio is connected, so ' + 'there is no radio to scope it to', + owner: 'lib/storage/non_radio_device_store.dart', + ), StorageSurface( 'radio_presets_v1_', verdict: ScopingVerdict.globalOnPurpose, diff --git a/lib/widgets/device_tile.dart b/lib/widgets/device_tile.dart index 36fef725..d9f2df96 100644 --- a/lib/widgets/device_tile.dart +++ b/lib/widgets/device_tile.dart @@ -8,7 +8,17 @@ class DeviceTile extends StatelessWidget { final ScanResult scanResult; final VoidCallback onTap; - const DeviceTile({super.key, required this.scanResult, required this.onTap}); + /// True when this entry came from the OS systemDevices query (connected via + /// GATT, not advertising) rather than a live scan. Such entries have no real + /// rssi, so the tile shows a connected badge instead of a signal bar (#570). + final bool systemConnected; + + const DeviceTile({ + super.key, + required this.scanResult, + required this.onTap, + this.systemConnected = false, + }); @override Widget build(BuildContext context) { @@ -21,7 +31,9 @@ class DeviceTile extends StatelessWidget { final name = adv.isNotEmpty ? adv : device.platformName; return ListTile( - leading: _buildSignalIcon(rssi), + leading: systemConnected + ? _buildConnectedBadge(context) + : _buildSignalIcon(rssi), title: Text( name.isNotEmpty ? name : context.l10n.common_unknownDevice, style: const TextStyle(fontWeight: FontWeight.w500), @@ -35,6 +47,22 @@ class DeviceTile extends StatelessWidget { ); } + // #570: OS-connected entries have no advertisement, so rssi is a sentinel. + // Show a neutral connected badge, never a (green) signal bar for a fake 0. + Widget _buildConnectedBadge(BuildContext context) { + final color = Theme.of(context).colorScheme.primary; + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon(Icons.bluetooth_connected, color: color), + Text( + context.l10n.scanner_connectedBadge, + style: TextStyle(fontSize: 10, color: color), + ), + ], + ); + } + Widget _buildSignalIcon(int rssi) { final tier = rssi >= -60 ? 0 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-')); + }); + }); +} diff --git a/test/connector/device_name_prefixes_test.dart b/test/connector/device_name_prefixes_test.dart new file mode 100644 index 00000000..4e77aea1 --- /dev/null +++ b/test/connector/device_name_prefixes_test.dart @@ -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', + ); + }); +} diff --git a/untranslated.json b/untranslated.json index 39961e4a..b1752e97 100644 --- a/untranslated.json +++ b/untranslated.json @@ -4,6 +4,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -480,6 +483,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -956,6 +962,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -1432,6 +1441,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -1908,6 +1920,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -2384,6 +2399,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -2860,6 +2878,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -3336,6 +3357,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -3812,6 +3836,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -4288,6 +4315,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -4764,6 +4794,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -5240,6 +5273,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -5716,6 +5752,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -6192,6 +6231,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -6668,6 +6710,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -7144,6 +7189,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient", @@ -7620,6 +7668,9 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_connectedBadge", + "scanner_connectedToPhone", + "scanner_connectedToPhoneHint", "scanner_scanFailedLocationOff", "scanner_scanFailedGeneric", "scanner_connectFailedTransient",