diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1fc60b9..6657d19 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -31,6 +31,7 @@ + diff --git a/app/src/main/java/com/example/medilightv2android/services/BleForegroundService.kt b/app/src/main/java/com/example/medilightv2android/services/BleForegroundService.kt index 73ab087..aead786 100644 --- a/app/src/main/java/com/example/medilightv2android/services/BleForegroundService.kt +++ b/app/src/main/java/com/example/medilightv2android/services/BleForegroundService.kt @@ -29,6 +29,9 @@ class BleForegroundService : Service() { const val ACTION_STOP = "com.example.medilightv2android.BLE_STOP" fun start(context: Context) { + // 배터리 최적화 제외 요청 (Doze 면제) + requestBatteryOptimizationExemption(context) + val intent = Intent(context, BleForegroundService::class.java).apply { action = ACTION_START } @@ -39,6 +42,22 @@ class BleForegroundService : Service() { } } + @android.annotation.SuppressLint("BatteryLife") + private fun requestBatteryOptimizationExemption(context: Context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager + if (!pm.isIgnoringBatteryOptimizations(context.packageName)) { + try { + val intent = Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply { + data = android.net.Uri.parse("package:${context.packageName}") + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + context.startActivity(intent) + } catch (_: Exception) {} + } + } + } + fun stop(context: Context) { val intent = Intent(context, BleForegroundService::class.java).apply { action = ACTION_STOP