Check if client has been initialized when clicking back button

This commit is contained in:
Sylwester Zieliński
2023-08-10 12:36:07 +02:00
parent f807e2fb87
commit 2bbfe21abd
7 changed files with 34 additions and 26 deletions

View File

@@ -71,7 +71,7 @@ internal class UARTService : NotificationService() {
@Inject
lateinit var repository: UARTRepository
private lateinit var client: ClientBleGatt
private var client: ClientBleGatt? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
@@ -90,7 +90,8 @@ internal class UARTService : NotificationService() {
}
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
client = ClientBleGatt.connect(this@UARTService, device, logger = { p, s -> repository.log(p, s) })
val client = ClientBleGatt.connect(this@UARTService, device, logger = { p, s -> repository.log(p, s) })
this@UARTService.client = client
if (!client.isConnected) {
return@launch
@@ -155,7 +156,7 @@ internal class UARTService : NotificationService() {
}
private fun disconnect() {
client.disconnect()
client?.disconnect()
}
override fun onDestroy() {