fix(ios): open settings from pairing recovery

This commit is contained in:
2026-04-28 20:06:10 +02:00
parent 2e7c10f87d
commit 9922b90f49
5 changed files with 29 additions and 43 deletions

View File

@ -1,17 +1,11 @@
package com.example.abawo_bt_app package com.example.abawo_bt_app
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity 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.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins import io.reactivex.plugins.RxJavaPlugins
class MainActivity: FlutterActivity() { class MainActivity: FlutterActivity() {
private val settingsChannel = "abawo/settings"
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
RxJavaPlugins.setErrorHandler { throwable -> RxJavaPlugins.setErrorHandler { throwable ->
val error = if (throwable is UndeliverableException && throwable.cause != null) { val error = if (throwable is UndeliverableException && throwable.cause != null) {
@ -29,27 +23,4 @@ class MainActivity: FlutterActivity() {
} }
super.onCreate(savedInstanceState) 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()
}
}
}
} }

View File

@ -1,31 +1,35 @@
import 'dart:io'; import 'dart:io';
import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
const MethodChannel _settingsChannel = MethodChannel('abawo/settings');
Future<bool> openBluetoothSettings() async { Future<bool> openBluetoothSettings() async {
if (!Platform.isAndroid) {
return false;
}
try { try {
return await _settingsChannel.invokeMethod<bool>('openBluetoothSettings') ?? if (Platform.isAndroid) {
false; await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
} on PlatformException { } else if (Platform.isIOS) {
await AppSettings.openAppSettings();
} else {
return false;
}
return true;
} catch (_) {
return false; return false;
} }
} }
Future<void> showBluetoothPairingRecoveryDialog(BuildContext context) { Future<void> 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<void>( return showDialog<void>(
context: context, context: context,
builder: (context) => AlertDialog( builder: (context) => AlertDialog(
title: const Text('Bluetooth pairing may be broken'), title: const Text('Bluetooth pairing may be broken'),
content: const Text( content: Text(content),
'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.',
),
actions: [ actions: [
TextButton( TextButton(
onPressed: () => Navigator.of(context).pop(), onPressed: () => Navigator.of(context).pop(),
@ -36,7 +40,7 @@ Future<void> showBluetoothPairingRecoveryDialog(BuildContext context) {
Navigator.of(context).pop(); Navigator.of(context).pop();
await openBluetoothSettings(); await openBluetoothSettings();
}, },
child: const Text('Open Bluetooth settings'), child: Text(settingsButtonLabel),
), ),
], ],
), ),

View File

@ -5,6 +5,7 @@
import FlutterMacOS import FlutterMacOS
import Foundation import Foundation
import app_settings
import connectivity_plus import connectivity_plus
import file_picker import file_picker
import flutter_blue_plus_darwin import flutter_blue_plus_darwin
@ -15,6 +16,7 @@ import shared_preferences_foundation
import sqlite3_flutter_libs import sqlite3_flutter_libs
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppSettingsPlugin.register(with: registry.registrar(forPlugin: "AppSettingsPlugin"))
ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin")) ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin"))
FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin")) FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin"))
FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin")) FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin"))

View File

@ -33,6 +33,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.0.1" 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: args:
dependency: transitive dependency: transitive
description: description:

View File

@ -54,6 +54,7 @@ dependencies:
flutter_reactive_ble: ^5.4.0 flutter_reactive_ble: ^5.4.0
nb_utils: ^7.2.0 nb_utils: ^7.2.0
file_picker: ^8.1.7 file_picker: ^8.1.7
app_settings: ^7.0.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: