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:
@@ -31,6 +31,7 @@
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
|
||||
<!-- BLE feature -->
|
||||
<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"
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user