Skip to content

Commit 7109bc6

Browse files
authored
[Bugfix] Various bugs fixed (#200)
* Catching exception when removing bond * Catching exception when uprooting trees * Hopefully fixed crash when service is started as foreground * BLEK and Commons libs updated with bugs fixed
1 parent aa91b1b commit 7109bc6

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

lib_service/src/main/java/no/nordicsemi/android/service/NotificationService.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ abstract class NotificationService : LifecycleService() {
7474
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
7575
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
7676
val notification = createNotification(R.string.device_connected_message)
77-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
78-
startForeground(NOTIFICATION_ID, notification)
79-
} else {
80-
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
81-
nm.notify(NOTIFICATION_ID, notification)
82-
}
77+
startForeground(NOTIFICATION_ID, notification)
8378
}
8479

8580
/**

lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ internal class ProfileService : NotificationService() {
7272

7373
override fun onDestroy() {
7474
managedConnections.values.forEach { it.cancel() }
75-
loggers.values.forEach { Timber.uproot(it) }
75+
loggers.values.forEach {
76+
try {
77+
Timber.uproot(it)
78+
} catch (_: IllegalArgumentException) {
79+
// Already uprooted, ignore.
80+
}
81+
}
7682
super.onDestroy()
7783
}
7884

@@ -236,7 +242,13 @@ internal class ProfileService : NotificationService() {
236242
* Uproots and clears the logger for the given device.
237243
*/
238244
private fun uprootLogger(deviceAddress: String) {
239-
loggers[deviceAddress]?.let { Timber.uproot(it) }
245+
loggers[deviceAddress]?.let {
246+
try {
247+
Timber.uproot(it)
248+
} catch (_: IllegalArgumentException) {
249+
// Already uprooted, ignore.
250+
}
251+
}
240252
loggers.remove(deviceAddress)
241253
}
242254

@@ -259,7 +271,12 @@ internal class ProfileService : NotificationService() {
259271
}
260272

261273
override suspend fun forget(address: String) {
262-
getPeripheral(address).removeBond()
274+
try {
275+
// This method may throw: OperationFailedException(reason=Request Failed)
276+
getPeripheral(address).removeBond()
277+
} catch (e: Exception) {
278+
Timber.tag(address).e("Failed to remove bond information: ${e.message}")
279+
}
263280
}
264281
}
265282

lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileServiceManager.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.ComponentName
44
import android.content.Context
55
import android.content.Intent
66
import android.content.ServiceConnection
7+
import android.os.Build
78
import android.os.IBinder
89
import dagger.hilt.android.qualifiers.ApplicationContext
910
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -51,6 +52,10 @@ internal class ProfileServiceManagerImp @Inject constructor(
5152
val intent = Intent(context, ProfileService::class.java)
5253
intent.putExtra(ProfileService.DEVICE_ADDRESS, deviceAddress)
5354
intent.putExtra(ProfileService.DEVICE_NAME, deviceName)
54-
context.startService(intent)
55+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
56+
context.startForegroundService(intent)
57+
} else {
58+
context.startService(intent)
59+
}
5560
}
5661
}

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ dependencyResolutionManagement {
5656
}
5757
// Fixed versions for Nordic libraries.
5858
create("nordic") {
59-
from("no.nordicsemi.gradle:nordic-version-catalog:2026.07.02")
59+
from("no.nordicsemi.gradle:nordic-version-catalog:2026.07.03")
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)