fix: 배터리 최적화 제외 요청 (Doze 면제, 삼성 대응)

- BLE 연결 시 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 다이얼로그 표시
- 이미 제외 상태면 스킵
- Manifest에 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 권한 추가

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-06 12:30:54 +09:00
parent 4357cb359a
commit b4e2b21306
2 changed files with 20 additions and 0 deletions
+1
View File
@@ -31,6 +31,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<!-- BLE feature --> <!-- BLE feature -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" /> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
@@ -29,6 +29,9 @@ class BleForegroundService : Service() {
const val ACTION_STOP = "com.example.medilightv2android.BLE_STOP" const val ACTION_STOP = "com.example.medilightv2android.BLE_STOP"
fun start(context: Context) { fun start(context: Context) {
// 배터리 최적화 제외 요청 (Doze 면제)
requestBatteryOptimizationExemption(context)
val intent = Intent(context, BleForegroundService::class.java).apply { val intent = Intent(context, BleForegroundService::class.java).apply {
action = ACTION_START 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) { fun stop(context: Context) {
val intent = Intent(context, BleForegroundService::class.java).apply { val intent = Intent(context, BleForegroundService::class.java).apply {
action = ACTION_STOP action = ACTION_STOP