|
|
|
@ -201,10 +201,10 @@ class PairingController extends GetxController { |
|
|
|
// iOS需要请求的权限 |
|
|
|
permissionsToRequest.add(Permission.bluetooth); |
|
|
|
permissionsToRequest.add(Permission.location); |
|
|
|
|
|
|
|
|
|
|
|
// 如果需要麦克风权限(例如语音功能) |
|
|
|
// permissionsToRequest.add(Permission.microphone); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
// 先尝试请求权限 |
|
|
|
if (permissionsToRequest.isNotEmpty) { |
|
|
|
@ -214,17 +214,18 @@ class PairingController extends GetxController { |
|
|
|
if (!status.isGranted) { |
|
|
|
allGranted = false; |
|
|
|
Logger.w(_tag, 'iOS权限 ${permission.toString()} 未被授予: $status'); |
|
|
|
|
|
|
|
|
|
|
|
// 特殊处理蓝牙权限 |
|
|
|
if (permission == Permission.bluetooth) { |
|
|
|
Logger.w(_tag, 'iOS蓝牙权限未授予,状态: $status'); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 如果有权限未授予,但不是永久拒绝,继续尝试 |
|
|
|
if (!allGranted) { |
|
|
|
bool hasPermanentlyDenied = statuses.values.any((status) => status.isPermanentlyDenied); |
|
|
|
bool hasPermanentlyDenied = |
|
|
|
statuses.values.any((status) => status.isPermanentlyDenied); |
|
|
|
if (hasPermanentlyDenied) { |
|
|
|
// 有权限被永久拒绝,需要引导用户去设置 |
|
|
|
_showPermissionSettingsDialog('部分权限被永久拒绝,请在设置中启用'); |
|
|
|
@ -232,15 +233,16 @@ class PairingController extends GetxController { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 直接尝试使用蓝牙功能,这会触发iOS原生权限请求 |
|
|
|
bool isEnabled = await _bluetoothService.isBluetoothEnabled(); |
|
|
|
Logger.i(_tag, 'iOS蓝牙状态检查: ${isEnabled ? "已开启" : "未开启"}'); |
|
|
|
|
|
|
|
|
|
|
|
// 检测AccessorySetupKit支持情况(iOS 18+特性) |
|
|
|
bool companionSupported = await _bleService.isCompanionSupported(); |
|
|
|
Logger.i(_tag, 'iOS Companion支持检查: ${companionSupported ? "支持" : "不支持"}'); |
|
|
|
|
|
|
|
Logger.i( |
|
|
|
_tag, 'iOS Companion支持检查: ${companionSupported ? "支持" : "不支持"}'); |
|
|
|
|
|
|
|
if (companionSupported) { |
|
|
|
// 针对iOS 18+,尝试预热AccessorySetupKit框架 |
|
|
|
try { |
|
|
|
@ -251,23 +253,23 @@ class PairingController extends GetxController { |
|
|
|
Logger.w(_tag, 'iOS 18+ AccessorySetupKit初始化失败: ${e.toString()}'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 在iOS上视为权限已授予 |
|
|
|
_hasPermissions.value = true; |
|
|
|
return true; |
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, "iOS蓝牙初始化失败: ${e.toString()}"); |
|
|
|
|
|
|
|
|
|
|
|
// 显示自定义对话框引导用户 |
|
|
|
_showPermissionSettingsDialog('需要开启蓝牙权限才能配对设备'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还有其他权限需要请求 |
|
|
|
if (permissionsToRequest.isNotEmpty) { |
|
|
|
statuses = await permissionsToRequest.request(); |
|
|
|
|
|
|
|
|
|
|
|
bool allGranted = true; |
|
|
|
statuses.forEach((permission, status) { |
|
|
|
if (!status.isGranted) { |
|
|
|
@ -275,7 +277,7 @@ class PairingController extends GetxController { |
|
|
|
Logger.w(_tag, '权限 ${permission.toString()} 未被授予: $status'); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!allGranted) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
@ -284,7 +286,7 @@ class PairingController extends GetxController { |
|
|
|
_hasPermissions.value = true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 显示权限设置对话框 |
|
|
|
void _showPermissionSettingsDialog(String message) { |
|
|
|
Get.dialog( |
|
|
|
@ -537,27 +539,27 @@ class PairingController extends GetxController { |
|
|
|
try { |
|
|
|
isLoading.value = true; |
|
|
|
statusDescription.value = '正在重新初始化蓝牙...'; |
|
|
|
|
|
|
|
|
|
|
|
// iOS上尝试重置权限状态 |
|
|
|
if (Platform.isIOS) { |
|
|
|
await Future.delayed(Duration(milliseconds: 500)); |
|
|
|
|
|
|
|
|
|
|
|
// 检查蓝牙权限 |
|
|
|
PermissionStatus bluetoothStatus = await Permission.bluetooth.status; |
|
|
|
Logger.i(_tag, "iOS蓝牙权限当前状态: $bluetoothStatus"); |
|
|
|
|
|
|
|
|
|
|
|
if (bluetoothStatus.isPermanentlyDenied) { |
|
|
|
Logger.i(_tag, "iOS蓝牙权限处于永久拒绝状态,提示用户手动开启"); |
|
|
|
_showPermissionSettingsDialog('蓝牙权限被拒绝,请在设置中手动开启'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 尝试重新请求权限 |
|
|
|
if (!bluetoothStatus.isGranted) { |
|
|
|
bluetoothStatus = await Permission.bluetooth.request(); |
|
|
|
Logger.i(_tag, "iOS蓝牙权限请求结果: $bluetoothStatus"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 尝试检查蓝牙状态,这会重新触发系统权限弹窗 |
|
|
|
try { |
|
|
|
bool isEnabled = await _bluetoothService.isBluetoothEnabled(); |
|
|
|
@ -565,7 +567,7 @@ class PairingController extends GetxController { |
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, "iOS蓝牙状态检查失败: ${e.toString()}"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查Companion功能(iOS 18+) |
|
|
|
try { |
|
|
|
bool isSupported = await _bleService.isCompanionSupported(); |
|
|
|
@ -578,7 +580,7 @@ class PairingController extends GetxController { |
|
|
|
Logger.w(_tag, "iOS Companion功能检查失败: ${e.toString()}"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 重新检查权限 |
|
|
|
if (await _checkAndRequestPermissions()) { |
|
|
|
// 重新检查蓝牙状态 |
|
|
|
@ -591,7 +593,7 @@ class PairingController extends GetxController { |
|
|
|
_updateStatusDescription(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 打开应用设置 |
|
|
|
Future<void> openAppSettings() async { |
|
|
|
try { |
|
|
|
@ -617,10 +619,10 @@ class PairingController extends GetxController { |
|
|
|
// 即使没有设备连接,也启动BLE后台服务 |
|
|
|
await _startAgentService(); |
|
|
|
_isNavigatingToHome = true; |
|
|
|
|
|
|
|
|
|
|
|
// 记录跳过配对的日志 |
|
|
|
Logger.i(_tag, '用户跳过设备配对,直接跳转到主页'); |
|
|
|
|
|
|
|
|
|
|
|
Get.offAllNamed(Routes.home); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|