From d2b92f9bbcae6ef96cfc53d68ccdf11910ce40a7 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:27:51 -0400 Subject: [PATCH] fix(#1033): tell the user when a Bluetooth scan cannot start On Android 11 and below the BLE plugin refuses to scan while system Location is off. The Scan button caught that error and only logged it, so the user saw nothing; one tapped Scan 172 times. A failed scan now shows a persistent message: a specific one when Location is off, a plain generic one for anything else, never the raw exception. The detail still goes to the log, and Scan stays pressable. New owner-approved strings scanner_scanFailedLocationOff and scanner_scanFailedGeneric; other locales fall back to English via untranslated.json. Tests: each message present on its failure, absent on success, still shown after 10 s, and Scan retries after a failure. Each check was made to fail first against a broken build (persist false; log-only; misclassification). Plan by EmeraldBrook on #1033. Agent: BrightDog (session 38fb0e08) Co-Authored-By: Claude Opus 5.5 --- lib/l10n/app_en.arb | 8 ++ lib/l10n/app_localizations.dart | 12 ++ lib/l10n/app_localizations_bg.dart | 8 ++ lib/l10n/app_localizations_de.dart | 8 ++ lib/l10n/app_localizations_en.dart | 8 ++ lib/l10n/app_localizations_es.dart | 8 ++ lib/l10n/app_localizations_fr.dart | 8 ++ lib/l10n/app_localizations_hu.dart | 8 ++ lib/l10n/app_localizations_it.dart | 8 ++ lib/l10n/app_localizations_ja.dart | 8 ++ lib/l10n/app_localizations_ko.dart | 8 ++ lib/l10n/app_localizations_nl.dart | 8 ++ lib/l10n/app_localizations_pl.dart | 8 ++ lib/l10n/app_localizations_pt.dart | 8 ++ lib/l10n/app_localizations_ru.dart | 8 ++ lib/l10n/app_localizations_sk.dart | 8 ++ lib/l10n/app_localizations_sl.dart | 8 ++ lib/l10n/app_localizations_sv.dart | 8 ++ lib/l10n/app_localizations_uk.dart | 8 ++ lib/l10n/app_localizations_zh.dart | 8 ++ lib/screens/scanner_screen.dart | 37 +++++- test/screens/scanner_scan_error_test.dart | 146 ++++++++++++++++++++++ untranslated.json | 34 +++++ 23 files changed, 376 insertions(+), 5 deletions(-) create mode 100644 test/screens/scanner_scan_error_test.dart diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 8c5982c6..8960c537 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -158,6 +158,14 @@ "scanner_scan": "Scan", "scanner_bluetoothOff": "Bluetooth is off", "scanner_bluetoothOffMessage": "Please turn on Bluetooth to scan for devices", + "scanner_scanFailedLocationOff": "Location is off, so Android won't let the app scan for Bluetooth radios. Turn on Location in your phone's quick settings, then tap Scan again.", + "@scanner_scanFailedLocationOff": { + "description": "Shown when a Bluetooth scan is refused because system Location is off (Android 11 and below) (#1033)." + }, + "scanner_scanFailedGeneric": "Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.", + "@scanner_scanFailedGeneric": { + "description": "Shown for any other failure to start a Bluetooth scan; the raw error goes to the log only (#1033)." + }, "scanner_connectFailedTransient": "The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.", "@scanner_connectFailedTransient": { "description": "Human-readable text for an Android GATT 133 connect failure after the bounded retry also failed (#698)." diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index 183ec5ad..c8b31e00 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -664,6 +664,18 @@ abstract class AppLocalizations { /// **'Please turn on Bluetooth to scan for devices'** String get scanner_bluetoothOffMessage; + /// Shown when a Bluetooth scan is refused because system Location is off (Android 11 and below) (#1033). + /// + /// In en, this message translates to: + /// **'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'** + String get scanner_scanFailedLocationOff; + + /// Shown for any other failure to start a Bluetooth scan; the raw error goes to the log only (#1033). + /// + /// In en, this message translates to: + /// **'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'** + String get scanner_scanFailedGeneric; + /// Human-readable text for an Android GATT 133 connect failure after the bounded retry also failed (#698). /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index 04b4f11e..4aef2fb4 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -307,6 +307,14 @@ class AppLocalizationsBg extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Моля, активирайте Bluetooth, за да сканирате за устройства.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index 3a6cf7d6..2b8d59ac 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -310,6 +310,14 @@ class AppLocalizationsDe extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Bitte aktivieren Sie Bluetooth, um nach Geräten zu suchen.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index 89ec7ad9..05136266 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -304,6 +304,14 @@ class AppLocalizationsEn extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Please turn on Bluetooth to scan for devices'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index 80f34f31..be9d4a3e 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -308,6 +308,14 @@ class AppLocalizationsEs extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Por favor, active el Bluetooth para escanear dispositivos.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index fad58955..6738f5f9 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -310,6 +310,14 @@ class AppLocalizationsFr extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Veuillez activer le Bluetooth pour rechercher des appareils.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index 16bbd431..e5bd2129 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -307,6 +307,14 @@ class AppLocalizationsHu extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Kérjük, kapcsolja be a Bluetooth-ot, hogy eszközök keresése lehessen.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index c66334c6..b1978d2b 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -310,6 +310,14 @@ class AppLocalizationsIt extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Si prega di attivare il Bluetooth per effettuare la scansione dei dispositivi.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index b00753e1..c5638629 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -297,6 +297,14 @@ class AppLocalizationsJa extends AppLocalizations { @override String get scanner_bluetoothOffMessage => 'Bluetoothを有効にして、デバイスを検索してください。'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index 1dc3e323..eef52521 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -296,6 +296,14 @@ class AppLocalizationsKo extends AppLocalizations { @override String get scanner_bluetoothOffMessage => '블루투스를 켜서 장치를 검색해주세요.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index e07c0332..b3f00605 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -306,6 +306,14 @@ class AppLocalizationsNl extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Zorg ervoor dat Bluetooth is ingeschakeld om naar apparaten te zoeken.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 4617a92b..b1962d3b 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -311,6 +311,14 @@ class AppLocalizationsPl extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Prosimy włączyć Bluetooth, aby przeskanować urządzenia.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index 97c824e7..b3b57b10 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -309,6 +309,14 @@ class AppLocalizationsPt extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Por favor, ative o Bluetooth para escanear por dispositivos.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index 64167d98..6748532e 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -309,6 +309,14 @@ class AppLocalizationsRu extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Пожалуйста, включите Bluetooth, чтобы найти устройства.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 4f7a8e30..0d7e01ce 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -308,6 +308,14 @@ class AppLocalizationsSk extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Prosím, zapnite Bluetooth, aby ste mohli skenovať pre zariadenia.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index ab714500..e90ee0f6 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -306,6 +306,14 @@ class AppLocalizationsSl extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Prosimo, vklopite Bluetooth, da lahko poiščete naprave.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 176b957f..f395a517 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -305,6 +305,14 @@ class AppLocalizationsSv extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Vänligen aktivera Bluetooth för att söka efter enheter.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index bff80ca7..c9ec29de 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -308,6 +308,14 @@ class AppLocalizationsUk extends AppLocalizations { String get scanner_bluetoothOffMessage => 'Будь ласка, увімкніть Bluetooth, щоб сканувати пристрої.'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 9407d34c..a397f517 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -294,6 +294,14 @@ class AppLocalizationsZh extends AppLocalizations { @override String get scanner_bluetoothOffMessage => '请开启蓝牙以搜索设备'; + @override + String get scanner_scanFailedLocationOff => + 'Location is off, so Android won\'t let the app scan for Bluetooth radios. Turn on Location in your phone\'s quick settings, then tap Scan again.'; + + @override + String get scanner_scanFailedGeneric => + 'Could not start the Bluetooth scan. Tap Scan to try again. If it keeps failing, turn Bluetooth off and on.'; + @override String get scanner_connectFailedTransient => 'The radio refused the connection (a common transient Bluetooth error). It was already retried; wait a few seconds and try again.'; diff --git a/lib/screens/scanner_screen.dart b/lib/screens/scanner_screen.dart index fbb491f2..14b476b6 100644 --- a/lib/screens/scanner_screen.dart +++ b/lib/screens/scanner_screen.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../utils/platform_info.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:provider/provider.dart'; @@ -235,11 +236,8 @@ class _ScannerScreenState extends State { connector.stopScan(); } else { unawaited( - connector.startScan().catchError((e) { - appLogger.warn( - 'startScan error: $e', - tag: 'ScannerScreen', - ); + connector.startScan().catchError((Object e) { + _reportScanFailure(e); }), ); } @@ -421,6 +419,35 @@ class _ScannerScreenState extends State { } } + /// A scan that cannot start must say so (#1033). It used to be logged only, + /// so a phone with Location off showed nothing while every tap failed. The + /// detail goes to the log; the user gets a plain-language message that + /// stays until dismissed. Scan stays pressable: a later tap tries again. + void _reportScanFailure(Object e) { + appLogger.error('Scan failed: $e', tag: 'ScannerScreen'); + if (!mounted) return; + showDismissibleSnackBar( + context, + content: Text(_describeScanError(e)), + backgroundColor: Colors.red, + persist: true, + ); + } + + /// On Android 11 and below the BLE plugin refuses to scan while system + /// Location is off, with this exact message + /// (`FlutterBluePlusPlugin.java:505-507`). Anything else, including a + /// reworded plugin message, gets the generic text rather than nothing. + String _describeScanError(Object e) { + final l10n = context.l10n; + if (e is PlatformException && + e.code == 'startScan' && + (e.message ?? '').contains('Location services are required')) { + return l10n.scanner_scanFailedLocationOff; + } + return l10n.scanner_scanFailedGeneric; + } + String _describeConnectError(Object e) { final l10n = context.l10n; if (e is FlutterBluePlusException && e.code == 133) { diff --git a/test/screens/scanner_scan_error_test.dart b/test/screens/scanner_scan_error_test.dart new file mode 100644 index 00000000..787e348d --- /dev/null +++ b/test/screens/scanner_scan_error_test.dart @@ -0,0 +1,146 @@ +// Pins #1033: a Bluetooth scan that cannot start must tell the user, and stay +// on screen until dismissed. On Android 11 and below the BLE plugin refuses +// to scan while system Location is off; the scanner used to log that and show +// nothing, so a user tapped Scan 172 times without learning why. + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/l10n/app_localizations.dart'; +import 'package:meshcore_open/screens/scanner_screen.dart'; +import 'package:meshcore_open/services/app_settings_service.dart'; +import 'package:provider/provider.dart'; + +/// The exact error `FlutterBluePlusPlugin.java:505-507` raises when Location +/// is off. Pinned here so a plugin change to the wording fails this test. +final _locationOff = PlatformException( + code: 'startScan', + message: 'Location services are required for Bluetooth scan', +); + +class _ScanConnector extends MeshCoreConnector { + Object? failWith; + int startScanCalls = 0; + + @override + MeshCoreConnectionState get state => MeshCoreConnectionState.disconnected; + + @override + Future startScan({ + Duration timeout = const Duration(seconds: 10), + }) async { + startScanCalls++; + final error = failWith; + if (error != null) throw error; + } +} + +Future _pumpScanner( + WidgetTester tester, + _ScanConnector connector, +) async { + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: connector), + ChangeNotifierProvider( + create: (_) => AppSettingsService(), + ), + ], + child: MaterialApp( + locale: const Locale('en'), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: const ScannerScreen(), + ), + ), + ); + await tester.pumpAndSettle(); + return AppLocalizations.of(tester.element(find.byType(ScannerScreen))); +} + +Future _tapScan(WidgetTester tester, AppLocalizations l10n) async { + await tester.tap( + find.widgetWithText(FloatingActionButton, l10n.scanner_scan), + ); + // Let the failure land, then finish the entrance animation, which is what + // starts a non-persistent message's timeout. + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); +} + +/// Well past the 4 second default. Settle afterwards, or a timed-out message +/// is caught mid-exit, still found, and the check could never fail. +Future _waitPastTimeout(WidgetTester tester) async { + await tester.pump(const Duration(seconds: 10)); + await tester.pumpAndSettle(); +} + +/// Drain ScannerScreen's dispose-time disconnect before teardown. +Future _unmount(WidgetTester tester) async { + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(const Duration(milliseconds: 60)); +} + +void main() { + testWidgets('Location off: says so, and the message stays', (tester) async { + final connector = _ScanConnector()..failWith = _locationOff; + final l10n = await _pumpScanner(tester, connector); + + await _tapScan(tester, l10n); + expect(connector.startScanCalls, 1); + expect(find.text(l10n.scanner_scanFailedLocationOff), findsOneWidget); + expect(find.text(l10n.scanner_scanFailedGeneric), findsNothing); + + await _waitPastTimeout(tester); + expect(find.text(l10n.scanner_scanFailedLocationOff), findsOneWidget); + await _unmount(tester); + }); + + testWidgets('any other scan failure: generic message, and it stays', ( + tester, + ) async { + final connector = _ScanConnector() + ..failWith = PlatformException(code: 'startScan', message: 'adapter'); + final l10n = await _pumpScanner(tester, connector); + + await _tapScan(tester, l10n); + expect(find.text(l10n.scanner_scanFailedGeneric), findsOneWidget); + expect(find.text(l10n.scanner_scanFailedLocationOff), findsNothing); + expect(find.textContaining('PlatformException'), findsNothing); + + await _waitPastTimeout(tester); + expect(find.text(l10n.scanner_scanFailedGeneric), findsOneWidget); + await _unmount(tester); + }); + + testWidgets('a scan that starts shows neither message', (tester) async { + final connector = _ScanConnector(); + final l10n = await _pumpScanner(tester, connector); + + await _tapScan(tester, l10n); + expect(connector.startScanCalls, 1); + expect(find.text(l10n.scanner_scanFailedLocationOff), findsNothing); + expect(find.text(l10n.scanner_scanFailedGeneric), findsNothing); + await _unmount(tester); + }); + + testWidgets('after a failure Scan is still pressable and tries again', ( + tester, + ) async { + final connector = _ScanConnector()..failWith = _locationOff; + final l10n = await _pumpScanner(tester, connector); + + await _tapScan(tester, l10n); + final scan = tester.widget( + find.widgetWithText(FloatingActionButton, l10n.scanner_scan), + ); + expect(scan.onPressed, isNotNull); + + await _tapScan(tester, l10n); + expect(connector.startScanCalls, 2); + expect(find.text(l10n.scanner_scanFailedLocationOff), findsOneWidget); + await _unmount(tester); + }); +} diff --git a/untranslated.json b/untranslated.json index 4d9c3a10..eb4bc5a4 100644 --- a/untranslated.json +++ b/untranslated.json @@ -4,6 +4,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -341,6 +343,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -678,6 +682,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -1015,6 +1021,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -1352,6 +1360,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -1689,6 +1699,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -2026,6 +2038,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -2363,6 +2377,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -2700,6 +2716,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -3037,6 +3055,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -3374,6 +3394,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -3711,6 +3733,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -4048,6 +4072,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -4385,6 +4411,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -4722,6 +4750,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -5059,6 +5089,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric", @@ -5396,6 +5428,8 @@ "scanner_reconnecting", "scanner_reconnectGaveUp", "connectBusy", + "scanner_scanFailedLocationOff", + "scanner_scanFailedGeneric", "scanner_connectFailedTransient", "scanner_connectFailedTimeout", "scanner_connectFailedGeneric",