feat: add bluetooth handle and listen for status
This commit is contained in:
parent
421b1719be
commit
cb93718491
@ -5,25 +5,55 @@ import android.bluetooth.BluetoothManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.IntentFilter
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.core.app.ComponentActivity
|
||||
import androidx.core.content.ContextCompat.getSystemService
|
||||
|
||||
object BluetoothService {
|
||||
private lateinit var bluetoothManager: BluetoothManager;
|
||||
private var bluetoothAdapter: BluetoothAdapter? = null;
|
||||
class BluetoothService(private val context: Context) {
|
||||
private var bluetoothManager: BluetoothManager =
|
||||
getSystemService(context, BluetoothManager::class.java)!!;
|
||||
private var bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
|
||||
private var bluetoothStateReceiver: BroadcastReceiver? = null
|
||||
|
||||
fun init(context: Context) {
|
||||
bluetoothManager = getSystemService(context, BluetoothManager::class.java)!!
|
||||
bluetoothAdapter = bluetoothManager.adapter
|
||||
|
||||
if (bluetoothAdapter == null) {
|
||||
throw Exception("Bluetooth not supported on this device")
|
||||
}
|
||||
init {
|
||||
registerBluetoothStateReceiver()
|
||||
}
|
||||
|
||||
fun isEnabled(): Boolean {
|
||||
return bluetoothAdapter?.isEnabled?: false
|
||||
return bluetoothAdapter.isEnabled ?: false
|
||||
}
|
||||
|
||||
private fun registerBluetoothStateReceiver() {
|
||||
bluetoothStateReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
|
||||
when (state) {
|
||||
BluetoothAdapter.STATE_ON -> {
|
||||
// Handle Bluetooth turned on
|
||||
println("SIGNAL: Bluetooth is enabled")
|
||||
}
|
||||
BluetoothAdapter.STATE_OFF -> {
|
||||
// Handle Bluetooth turned off
|
||||
println("SIGNAL: Bluetooth is disabled")
|
||||
}
|
||||
BluetoothAdapter.STATE_TURNING_ON -> {
|
||||
// Handle Bluetooth turning on
|
||||
println("SIGNAL: Bluetooth is turning on")
|
||||
}
|
||||
BluetoothAdapter.STATE_TURNING_OFF -> {
|
||||
// Handle Bluetooth turning off
|
||||
println("SIGNAL: Bluetooth is turning off")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.registerReceiver(bluetoothStateReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
||||
}
|
||||
|
||||
fun cleanUp() {
|
||||
context.unregisterReceiver(bluetoothStateReceiver)
|
||||
}
|
||||
}
|
||||
@ -33,9 +33,11 @@ import com.example.tvcontroller.ui.AppViewModel
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var bluetoothService: BluetoothService
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
BluetoothService.init(this)
|
||||
bluetoothService = BluetoothService(this.applicationContext)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
TVControllerTheme {
|
||||
@ -43,6 +45,11 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
bluetoothService.cleanUp()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@ -50,7 +57,6 @@ fun TvControllerApp(
|
||||
navController: NavHostController = rememberNavController(),
|
||||
appViewModel: AppViewModel = viewModel()
|
||||
) {
|
||||
val appUiState by appViewModel.uiState.collectAsState();
|
||||
val backStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentScreen = Screen.valueOf(backStackEntry?.destination?.route ?: Screen.Camera.name)
|
||||
val baselineCamera24 = painterResource(R.drawable.baseline_camera_24)
|
||||
@ -150,4 +156,4 @@ fun SettingsScreen(modifier: Modifier = Modifier) {
|
||||
) {
|
||||
Text(text = "Settings Screen", modifier = modifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,13 @@
|
||||
package com.example.tvcontroller.ui
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.example.tvcontroller.BluetoothService
|
||||
import com.example.tvcontroller.Screen
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
class AppViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(AppUiState())
|
||||
val uiState: StateFlow<AppUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun updateScreen(screen: Screen) {
|
||||
if (uiState.value.currentScreen == screen) return
|
||||
val newUiState = uiState.value.copy(currentScreen = screen)
|
||||
_uiState.update { newUiState }
|
||||
}
|
||||
class AppViewModel(private val bluetoothService: BluetoothService) : ViewModel() {
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user