202 lines
6.3 KiB
Dart
202 lines
6.3 KiB
Dart
import 'dart:io';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
|
||
class AppPermission {
|
||
// 检查权限
|
||
static Future<bool> checkPermission({required Permission permission}) async {
|
||
var status = await permission.status;
|
||
return status == PermissionStatus.granted;
|
||
}
|
||
|
||
// 批量检查权限
|
||
static Future<bool> checkPermissions({
|
||
required List<Permission> permissions,
|
||
}) async {
|
||
if (permissions.isEmpty) return false;
|
||
Map<Permission, PermissionStatus> statuses = await permissions.request();
|
||
for (var status in statuses.values) {
|
||
if (status != PermissionStatus.granted) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
static Future<bool> requestPermission({
|
||
required Permission permission,
|
||
}) async {
|
||
var status = await permission.request();
|
||
return status == PermissionStatus.granted;
|
||
}
|
||
|
||
static Future<bool> requestNotificationPermission() async {
|
||
final PermissionStatus status = await Permission.notification.request();
|
||
|
||
if (status == PermissionStatus.granted) {
|
||
print("通知权限已授予");
|
||
return true;
|
||
} else if (status == PermissionStatus.denied) {
|
||
// 第一次被拒绝,可以再次请求
|
||
return false;
|
||
} else if (status.isPermanentlyDenied) {
|
||
// 用户勾选了“不再提示”或被永久拒绝
|
||
print("权限被永久拒绝,跳转到设置页面");
|
||
openAppSettings(); // 跳转到应用设置页,手动开启
|
||
return false;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
// 请求蓝牙权限(Android 12及以上需要)
|
||
static Future<bool> requestBluetoothPermissions() async {
|
||
try {
|
||
// Android 12及以上需要的蓝牙权限
|
||
List<Permission> bluetoothPermissions = [];
|
||
|
||
if (Platform.isAndroid) {
|
||
// Android 12+ 需要细分权限
|
||
if (Platform.version.startsWith('Android 12') || Platform.isAndroid) {
|
||
bluetoothPermissions = [
|
||
Permission.bluetoothScan,
|
||
Permission.bluetoothConnect,
|
||
Permission.bluetoothAdvertise,
|
||
];
|
||
} else {
|
||
// Android 11 及以下
|
||
bluetoothPermissions = [
|
||
Permission.bluetooth,
|
||
Permission.location, // 扫描 BLE 可能需要位置
|
||
];
|
||
}
|
||
} else if (Platform.isIOS) {
|
||
// iOS 只需要通用 bluetooth 权限
|
||
bluetoothPermissions = [
|
||
Permission.bluetooth,
|
||
];
|
||
}
|
||
|
||
// Android 12以下需要的位置权限
|
||
List<Permission> locationPermissions = [
|
||
Permission.location,
|
||
Permission.locationWhenInUse,
|
||
];
|
||
|
||
bool bluetoothGranted = true;
|
||
bool locationGranted = true;
|
||
|
||
// 检查并请求蓝牙权限(Android 12+)
|
||
Map<Permission, PermissionStatus> bluetoothStatuses = await bluetoothPermissions.request();
|
||
for (var status in bluetoothStatuses.values) {
|
||
if (status != PermissionStatus.granted) {
|
||
bluetoothGranted = false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 检查并请求位置权限(Android 12以下或蓝牙扫描需要)
|
||
Map<Permission, PermissionStatus> locationStatuses = await locationPermissions.request();
|
||
for (var status in locationStatuses.values) {
|
||
if (status != PermissionStatus.granted) {
|
||
locationGranted = false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
bool hasPermission = bluetoothGranted && locationGranted;
|
||
|
||
if (hasPermission) {
|
||
print("蓝牙权限已授予");
|
||
} else {
|
||
print("蓝牙权限被拒绝");
|
||
}
|
||
|
||
return hasPermission;
|
||
} catch (e) {
|
||
print("请求蓝牙权限失败: $e");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 检查蓝牙扫描权限
|
||
static Future<bool> checkBluetoothScanPermission() async {
|
||
try {
|
||
var status = await Permission.bluetoothScan.status;
|
||
return status == PermissionStatus.granted;
|
||
} catch (e) {
|
||
// 如果权限不存在(Android 12以下),检查位置权限
|
||
return await checkLocationPermission();
|
||
}
|
||
}
|
||
|
||
// 检查蓝牙连接权限
|
||
static Future<bool> checkBluetoothConnectPermission() async {
|
||
try {
|
||
var status = await Permission.bluetoothConnect.status;
|
||
return status == PermissionStatus.granted;
|
||
} catch (e) {
|
||
// 如果权限不存在(Android 12以下),默认返回true
|
||
print("蓝牙连接权限检查失败,可能是Android 12以下版本: $e");
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// 检查蓝牙广播权限
|
||
static Future<bool> checkBluetoothAdvertisePermission() async {
|
||
try {
|
||
var status = await Permission.bluetoothAdvertise.status;
|
||
return status == PermissionStatus.granted;
|
||
} catch (e) {
|
||
// 如果权限不存在(Android 12以下),默认返回true
|
||
print("蓝牙广播权限检查失败,可能是Android 12以下版本: $e");
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// 检查位置权限(Android 12以下蓝牙扫描需要)
|
||
static Future<bool> checkLocationPermission() async {
|
||
var status = await Permission.location.status;
|
||
return status == PermissionStatus.granted;
|
||
}
|
||
|
||
// 请求位置权限
|
||
static Future<bool> requestLocationPermission() async {
|
||
try {
|
||
final PermissionStatus status = await Permission.location.request();
|
||
|
||
if (status == PermissionStatus.granted) {
|
||
print("位置权限已授予");
|
||
return true;
|
||
} else if (status == PermissionStatus.denied) {
|
||
print("位置权限被拒绝");
|
||
return false;
|
||
} else if (status.isPermanentlyDenied) {
|
||
print("位置权限被永久拒绝,跳转到设置页面");
|
||
return false;
|
||
}
|
||
|
||
return false;
|
||
} catch (e) {
|
||
print("请求位置权限失败: $e");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 检查所有蓝牙相关权限
|
||
static Future<bool> checkAllBluetoothPermissions() async {
|
||
if (Platform.isIOS) {
|
||
// iOS主要检查位置权限,蓝牙权限由系统自动处理
|
||
bool locationPermission = await checkLocationPermission();
|
||
return locationPermission;
|
||
} else {
|
||
// Android检查所有相关权限
|
||
bool scanPermission = await checkBluetoothScanPermission();
|
||
bool connectPermission = await checkBluetoothConnectPermission();
|
||
bool advertisePermission = await checkBluetoothAdvertisePermission();
|
||
bool locationPermission = await checkLocationPermission();
|
||
|
||
return scanPermission && connectPermission && advertisePermission && locationPermission;
|
||
}
|
||
}
|
||
}
|