fix(ios): open settings from pairing recovery
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<bool> openBluetoothSettings() async {
|
||||
if (!Platform.isAndroid) {
|
||||
try {
|
||||
if (Platform.isAndroid) {
|
||||
await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
|
||||
} else if (Platform.isIOS) {
|
||||
await AppSettings.openAppSettings();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return await _settingsChannel.invokeMethod<bool>('openBluetoothSettings') ??
|
||||
false;
|
||||
} on PlatformException {
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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>(
|
||||
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<void> showBluetoothPairingRecoveryDialog(BuildContext context) {
|
||||
Navigator.of(context).pop();
|
||||
await openBluetoothSettings();
|
||||
},
|
||||
child: const Text('Open Bluetooth settings'),
|
||||
child: Text(settingsButtonLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@ -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"))
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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:
|
||||
|
||||
Reference in New Issue
Block a user