diff --git a/android/app/src/main/kotlin/com/example/abawo_bt_app/MainActivity.kt b/android/app/src/main/kotlin/com/example/abawo_bt_app/MainActivity.kt index f45a606..6e5d181 100644 --- a/android/app/src/main/kotlin/com/example/abawo_bt_app/MainActivity.kt +++ b/android/app/src/main/kotlin/com/example/abawo_bt_app/MainActivity.kt @@ -1,17 +1,11 @@ package com.example.abawo_bt_app -import android.content.Intent import android.os.Bundle -import android.provider.Settings import io.flutter.embedding.android.FlutterActivity -import io.flutter.embedding.engine.FlutterEngine -import io.flutter.plugin.common.MethodChannel import io.reactivex.exceptions.UndeliverableException import io.reactivex.plugins.RxJavaPlugins class MainActivity: FlutterActivity() { - private val settingsChannel = "abawo/settings" - override fun onCreate(savedInstanceState: Bundle?) { RxJavaPlugins.setErrorHandler { throwable -> val error = if (throwable is UndeliverableException && throwable.cause != null) { @@ -29,27 +23,4 @@ class MainActivity: FlutterActivity() { } super.onCreate(savedInstanceState) } - - override fun configureFlutterEngine(flutterEngine: FlutterEngine) { - super.configureFlutterEngine(flutterEngine) - MethodChannel(flutterEngine.dartExecutor.binaryMessenger, settingsChannel) - .setMethodCallHandler { call, result -> - when (call.method) { - "openBluetoothSettings" -> { - try { - startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS)) - result.success(true) - } catch (_: Exception) { - try { - startActivity(Intent(Settings.ACTION_SETTINGS)) - result.success(true) - } catch (_: Exception) { - result.success(false) - } - } - } - else -> result.notImplemented() - } - } - } } diff --git a/lib/util/bluetooth_settings.dart b/lib/util/bluetooth_settings.dart index 2becda8..bcd5d2b 100644 --- a/lib/util/bluetooth_settings.dart +++ b/lib/util/bluetooth_settings.dart @@ -1,31 +1,35 @@ import 'dart:io'; +import 'package:app_settings/app_settings.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; - -const MethodChannel _settingsChannel = MethodChannel('abawo/settings'); Future openBluetoothSettings() async { - if (!Platform.isAndroid) { - return false; - } - try { - return await _settingsChannel.invokeMethod('openBluetoothSettings') ?? - false; - } on PlatformException { + if (Platform.isAndroid) { + await AppSettings.openAppSettings(type: AppSettingsType.bluetooth); + } else if (Platform.isIOS) { + await AppSettings.openAppSettings(); + } else { + return false; + } + return true; + } catch (_) { return false; } } Future showBluetoothPairingRecoveryDialog(BuildContext context) { + final isIOS = Platform.isIOS; + final content = isIOS + ? 'The connection opened, then broke while reading the device. This is probably a pairing problem.\n\nOn iOS, open Settings, go to Bluetooth, forget this device, then come back and connect again.' + : 'The connection opened, then broke while reading the device. This is probably a pairing problem.\n\nOpen Bluetooth settings, remove/forget this device, then come back and connect again.'; + final settingsButtonLabel = isIOS ? 'Open Settings' : 'Open Bluetooth settings'; + return showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Bluetooth pairing may be broken'), - content: const Text( - 'The connection opened, then broke while reading the device. This is probably a pairing problem.\n\nOpen Bluetooth settings, remove/forget this device, then come back and connect again.', - ), + content: Text(content), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), @@ -36,7 +40,7 @@ Future showBluetoothPairingRecoveryDialog(BuildContext context) { Navigator.of(context).pop(); await openBluetoothSettings(); }, - child: const Text('Open Bluetooth settings'), + child: Text(settingsButtonLabel), ), ], ), diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 451942c..60b1d4f 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import app_settings import connectivity_plus import file_picker import flutter_blue_plus_darwin @@ -15,6 +16,7 @@ import shared_preferences_foundation import sqlite3_flutter_libs func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + AppSettingsPlugin.register(with: registry.registrar(forPlugin: "AppSettingsPlugin")) ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin")) FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin")) FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 89dce08..650c5a3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -33,6 +33,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.1" + app_settings: + dependency: "direct main" + description: + name: app_settings + sha256: "64d50e666fd96ae90301bf71205f05019286f940ad6f5fed3d1be19c6af7546a" + url: "https://pub.dev" + source: hosted + version: "7.0.0" args: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b2c6efc..26f2cca 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -54,6 +54,7 @@ dependencies: flutter_reactive_ble: ^5.4.0 nb_utils: ^7.2.0 file_picker: ^8.1.7 + app_settings: ^7.0.0 dev_dependencies: flutter_test: