更新ios蓝牙权限

This commit is contained in:
wyf
2025-08-21 11:30:20 +08:00
parent 4fba3eb3a4
commit 5cb59bb5be
6 changed files with 164 additions and 127 deletions

View File

@@ -1,5 +1,8 @@
import 'dart:io';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
@@ -201,69 +204,92 @@ Future<Map> checkBlueToothPermissin() async {
var bluetoothConnectGranted;
var locationGranted;
try {
// 先显示权限说明弹窗(同屏显示)
bool bluetoothScanPermanentlyDenied =
await Permission.bluetoothScan.isPermanentlyDenied;
bool bluetoothConnectPermanentlyDenied =
await Permission.bluetoothConnect.isPermanentlyDenied;
bool locationPermanentlyDenied =
await Permission.location.isPermanentlyDenied;
if (bluetoothScanPermanentlyDenied ||
bluetoothConnectPermanentlyDenied ||
locationPermanentlyDenied) {
return data; // 直接返回,不再弹窗申请
if (Platform.isIOS) {
PermissionStatus isBleGranted = await Permission.bluetooth.request();
if (isBleGranted.isGranted) {
// startBluetoothScanning();
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
data = {
'bluetoothScanGranted': true,
'bluetoothConnectGranted': true,
};
} else {
try {
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
} catch (e) {
TopSlideNotification.show(Get.context!,
text: "蓝牙权限未开启,请在设置中开启蓝牙权限".tr,
textColor: themeController.currentColor.sc9);
}
}
if (userInfoController.initLocationpermission == 1) {
return data;
} else {
try {
// 先显示权限说明弹窗(同屏显示)
bool bluetoothScanPermanentlyDenied =
await Permission.bluetoothScan.isPermanentlyDenied;
bool bluetoothConnectPermanentlyDenied =
await Permission.bluetoothConnect.isPermanentlyDenied;
bool locationPermanentlyDenied =
await Permission.location.isPermanentlyDenied;
if (bluetoothScanPermanentlyDenied ||
bluetoothConnectPermanentlyDenied ||
locationPermanentlyDenied) {
return data; // 直接返回,不再弹窗申请
}
if (userInfoController.initLocationpermission == 1) {
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
locationGranted = await Permission.location.isGranted;
data = {
'bluetoothScanGranted': bluetoothScanGranted,
'bluetoothConnectGranted': bluetoothConnectGranted,
'locationGranted': locationGranted,
};
return data;
}
showPermissionInfoDialog(
Get.context!,
CommonVariables().bluetoothpermissionInfo,
);
show = true;
// 检查并请求权限
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
locationGranted = await Permission.location.isGranted;
if (!bluetoothScanGranted ||
!bluetoothConnectGranted ||
!locationGranted) {
await [
Permission.bluetoothScan,
Permission.bluetoothConnect,
Permission.location,
].request();
// 更新权限状态
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
locationGranted = await Permission.location.isGranted;
}
data = {
'bluetoothScanGranted': bluetoothScanGranted,
'bluetoothConnectGranted': bluetoothConnectGranted,
'locationGranted': locationGranted,
};
return data;
}
showPermissionInfoDialog(
Get.context!,
CommonVariables().bluetoothpermissionInfo,
);
show = true;
// 检查并请求权限
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
locationGranted = await Permission.location.isGranted;
if (!bluetoothScanGranted || !bluetoothConnectGranted || !locationGranted) {
await [
Permission.bluetoothScan,
Permission.bluetoothConnect,
Permission.location,
].request();
// 更新权限状态
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
locationGranted = await Permission.location.isGranted;
}
data = {
'bluetoothScanGranted': bluetoothScanGranted,
'bluetoothConnectGranted': bluetoothConnectGranted,
'locationGranted': locationGranted,
};
// 通知 web 端
WebviewTestController webviewTestController = Get.find();
// webviewTestController.web.jsbridge?.dart.updatePermission(data);
} catch (e) {
ef.log("蓝牙权限:$e");
} finally {
if (show) {
Navigator.of(Get.context!, rootNavigator: true).pop();
// 通知 web 端
WebviewTestController webviewTestController = Get.find();
// webviewTestController.web.jsbridge?.dart.updatePermission(data);
} catch (e) {
ef.log("蓝牙权限:$e");
} finally {
if (show) {
Navigator.of(Get.context!, rootNavigator: true).pop();
}
}
}