Autodisconnect from device, when the flight screen remove from the backstack

This commit is contained in:
2023-10-03 21:39:59 +07:00
parent ad13a7e958
commit 7436599ad3
3 changed files with 8 additions and 3 deletions

View File

@@ -241,7 +241,7 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro
if(dataTransferService == null) { if(dataTransferService == null) {
return null return null
} }
dataTransferService?.sendMessage("R1399\n\r".toByteArray()) dataTransferService?.sendMessage("R1250\n\r".toByteArray())
return message return message
} }

View File

@@ -1,6 +1,5 @@
package com.helible.pilot package com.helible.pilot
import android.bluetooth.BluetoothDevice
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@@ -84,7 +83,7 @@ class BluetoothViewModel(
} }
} }
} }
.catch { throwable -> .catch { _ ->
bluetoothController.closeConnection() bluetoothController.closeConnection()
_state.update { _state.update {
it.copy( it.copy(

View File

@@ -8,6 +8,7 @@ import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
@@ -162,6 +163,11 @@ class MainActivity : ComponentActivity() {
bluetoothViewModel.connectToDevice(device) bluetoothViewModel.connectToDevice(device)
} }
} }
BackHandler {
bluetoothViewModel.disconnectFromDevice()
Log.i("FlightScreen", "Disconnected from device")
navController.navigate("scanner")
}
when { when {
bluetoothState.isConnecting -> { bluetoothState.isConnecting -> {
Column( Column(