From f070495fb4a4081adb726f8450b90af4c8f4f75b Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Tue, 7 Sep 2021 21:11:42 +0200 Subject: [PATCH] Ask for permission before using Bluetooth --- src/main/AndroidManifest.xml | 6 +++- .../BluetoothLeSensorPreference.java | 36 +++++++++++++++++-- src/main/res/values-de/strings.xml | 8 +++-- src/main/res/values/strings.xml | 2 ++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index c4ffd9225..f8dcd78ca 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> @@ -52,7 +53,10 @@ limitations under the License. - + serviceUUIDs = getArguments().getParcelableArrayList(ARG_BLE_SERVICE_UUIDS); - // Don't know why: need to load the drawable _twice_, so that animation is actually started. bluetoothIcon = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.ic_bluetooth_searching_animated_24dp); bluetoothIcon = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.ic_bluetooth_searching_animated_24dp); bluetoothIcon.start(); + if ( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && ( + ContextCompat.checkSelfPermission(getContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED || + ContextCompat.checkSelfPermission(getContext(), Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED + ) + ) { + ActivityResultLauncher requestPermissionLauncher = + registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), granted -> { + if (!granted.containsValue(false)) { + startBluetoothScan(); + } else if (shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_SCAN) || shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_CONNECT)) { + Toast.makeText(getContext(), R.string.permission_bluetooth_failed_rejected, Toast.LENGTH_LONG).show(); + dismiss(); + } else { + Toast.makeText(getContext(), R.string.permission_bluetooth_failed, Toast.LENGTH_SHORT).show(); + dismiss(); + } + }); + + requestPermissionLauncher.launch(new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}); + } else { + startBluetoothScan(); + } + } + + private void startBluetoothScan() { + List serviceUUIDs = getArguments().getParcelableArrayList(ARG_BLE_SERVICE_UUIDS); + BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext()); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { Log.w(TAG, "Bluetooth adapter is present or not enabled."); diff --git a/src/main/res/values-de/strings.xml b/src/main/res/values-de/strings.xml index 70ae8a8af..b5c29c773 100644 --- a/src/main/res/values-de/strings.xml +++ b/src/main/res/values-de/strings.xml @@ -1,5 +1,4 @@ - - +--> + Über OpenTracks OpenTracks ist eine Sport-Tracking-Anwendung, die Ihre Privatsphäre vollständig respektiert. Projekt-Website: %1$s @@ -235,6 +235,8 @@ Meldet das GPS-Gerät ungenaue Daten (z.B. Standort, Geschwindigkeit, Höhe), ka Dienst für Aufzeichnung OpenTracks benötigt eine Berechtigung zur Nutzung von GPS. + OpenTracks benötigt eine Berechtigung zur Nutzung von Bluetooth. + Du musst OpenTracks die Berechtigung zum Zugriff auf Geräte in der Nähe manuell in den Systemeinstellungen geben. Akkustatus diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 86ecfa06a..2e78f7971 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -309,6 +309,8 @@ limitations under the License. Deletion service OpenTracks requires permission to use GPS. + OpenTracks requires permission to use Bluetooth. + You manually need to grant OpenTracks access to Nearby Devices in system settings. Battery level