|
|
|
@ -1,5 +1,7 @@ |
|
|
|
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 PermissionGroup { |
|
|
|
final String titleKey; |
|
|
|
@ -12,7 +14,37 @@ class PermissionGroup { |
|
|
|
required this.permissions, |
|
|
|
}); |
|
|
|
|
|
|
|
static List<PermissionGroup> getPlatformGroups() { |
|
|
|
/// 获取蓝牙权限列表(根据平台和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 []; |
|
|
|
} |
|
|
|
} else if (Platform.isIOS) { |
|
|
|
// iOS使用基本蓝牙权限 |
|
|
|
return [Permission.bluetooth]; |
|
|
|
} |
|
|
|
return []; |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取平台特定的权限组列表(异步方法) |
|
|
|
static Future<List<PermissionGroup>> getPlatformGroups() async { |
|
|
|
// 动态获取蓝牙权限列表 |
|
|
|
final bluetoothPermissions = await _getBluetoothPermissions(); |
|
|
|
|
|
|
|
return [ |
|
|
|
// 通讯权限组 - 仅Android(包含联系人权限) |
|
|
|
const PermissionGroup( |
|
|
|
@ -20,11 +52,11 @@ class PermissionGroup { |
|
|
|
icon: Icons.phone_android, |
|
|
|
permissions: [Permission.phone, Permission.sms, Permission.contacts], |
|
|
|
), |
|
|
|
// 蓝牙权限组 - Android 12+ |
|
|
|
const PermissionGroup( |
|
|
|
// 蓝牙权限组 - 根据平台动态获取权限 |
|
|
|
PermissionGroup( |
|
|
|
titleKey: 'bluetoothPermissions', |
|
|
|
icon: Icons.bluetooth, |
|
|
|
permissions: [Permission.bluetoothScan, Permission.bluetoothConnect], |
|
|
|
permissions: bluetoothPermissions, |
|
|
|
), |
|
|
|
// 通知权限组 |
|
|
|
const PermissionGroup( |
|
|
|
|