You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.0 KiB
98 lines
3.0 KiB
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
|
|
class PermissionGroupModel {
|
|
final String titleKey;
|
|
final IconData icon;
|
|
final List<Permission> permissions;
|
|
|
|
const PermissionGroupModel({
|
|
required this.titleKey,
|
|
required this.icon,
|
|
required this.permissions,
|
|
});
|
|
}
|
|
|
|
class PermissionGroup {
|
|
/// 获取蓝牙权限列表(根据平台和Android版本)
|
|
static Future<List<Permission>> _getBluetoothPermissions() async {
|
|
if (Platform.isAndroid) {
|
|
try {
|
|
final deviceInfo = DeviceInfoPlugin();
|
|
final androidInfo = await deviceInfo.androidInfo;
|
|
// Android 12+ (API 31+) 使用新的蓝牙权限
|
|
if (androidInfo.version.sdkInt >= 31) {
|
|
return [
|
|
Permission.bluetoothScan,
|
|
Permission.bluetoothConnect,
|
|
];
|
|
}
|
|
// Android 12以下不显示蓝牙权限(因为会自动授予)
|
|
return [];
|
|
} catch (e) {
|
|
print('获取Android版本信息失败: $e');
|
|
return [];
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/// 获取平台特定的权限组列表(异步方法)
|
|
static Future<List<PermissionGroupModel>> getPlatformGroups() async {
|
|
// 动态获取蓝牙权限列表
|
|
final bluetoothPermissions = await _getBluetoothPermissions();
|
|
|
|
return [
|
|
// 通讯权限组 - 仅Android(包含联系人权限)
|
|
const PermissionGroupModel(
|
|
titleKey: 'communicationPermissions',
|
|
icon: Icons.phone_android,
|
|
permissions: [Permission.phone, Permission.contacts],
|
|
),
|
|
// 蓝牙权限组 - 根据平台动态获取权限
|
|
PermissionGroupModel(
|
|
titleKey: 'bluetoothPermissions',
|
|
icon: Icons.bluetooth,
|
|
permissions: bluetoothPermissions,
|
|
),
|
|
// 通知权限组
|
|
const PermissionGroupModel(
|
|
titleKey: 'notificationPermissions',
|
|
icon: Icons.notifications,
|
|
permissions: [Permission.notification],
|
|
),
|
|
// 闹钟权限组 - 仅Android
|
|
const PermissionGroupModel(
|
|
titleKey: 'alarmPermissions',
|
|
icon: Icons.alarm,
|
|
permissions: [Permission.scheduleExactAlarm],
|
|
),
|
|
// 日历权限组
|
|
const PermissionGroupModel(
|
|
titleKey: 'calendarPermissions',
|
|
icon: Icons.calendar_today,
|
|
permissions: [Permission.calendarFullAccess],
|
|
),
|
|
// 位置权限组
|
|
const PermissionGroupModel(
|
|
titleKey: 'locationPermissions',
|
|
icon: Icons.location_on,
|
|
permissions: [Permission.location],
|
|
),
|
|
// 存储权限组
|
|
const PermissionGroupModel(
|
|
titleKey: 'storagePermissions',
|
|
icon: Icons.storage,
|
|
permissions: [Permission.storage],
|
|
),
|
|
// 麦克风权限组
|
|
const PermissionGroupModel(
|
|
titleKey: 'microphonePermissions',
|
|
icon: Icons.mic,
|
|
permissions: [Permission.microphone],
|
|
),
|
|
];
|
|
}
|
|
}
|
|
|