Add Real-Time Bluetooth State Monitoring to OnboardingFlowScreen (#55)

* Add bluetooth monitor function to onboarding flow screen

* Adds function that checks bluetooth status in real time

* Add bluetooth monitor function to onboarding flow screen

* Refactor

* Comment button to manually check BT state

* Original commits

* feat(ui): Remove auto-trigger for Bluetooth enable prompt

* feat(ui): Remove auto-trigger for Bluetooth enable prompt

* feat(ui): Remove BluetoothStatusBanner. Pending approval on feature, will add in a new PR

* Refactor and fix conflicts

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
Eben Justice
2025-08-05 23:49:04 +02:00
committed by GitHub
co-authored by callebtc
parent 335f914f32
commit a0d1c6f77c
3 changed files with 104 additions and 24 deletions
@@ -2,12 +2,15 @@ package com.bitchat.android.onboarding
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
/**
* Manages Bluetooth enable/disable state and user prompts
@@ -203,6 +206,59 @@ class BluetoothStatusManager(
fun logBluetoothStatus() {
Log.d(TAG, getDiagnostics())
}
/**
* Monitors Bluetooth state changes in real-time and invokes the provided callback.
* Returns the BroadcastReceiver for manual un-registration.
*/
fun monitorBluetoothState(
context: Context,
bluetoothStatusManager: BluetoothStatusManager,
onBluetoothStateChanged: (BluetoothStatus) -> Unit
): BroadcastReceiver {
Log.d(TAG, "Starting Bluetooth State Monitoring")
if (!bluetoothStatusManager.isBluetoothSupported()) {
Log.e(TAG, "Bluetooth is not supported")
onBluetoothStateChanged(BluetoothStatus.NOT_SUPPORTED)
bluetoothStatusManager.handleBluetoothStatus(BluetoothStatus.NOT_SUPPORTED)
return object : BroadcastReceiver() { override fun onReceive(p0: Context?, p1: Intent?) {}
}
}
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_ON -> {
Log.d(TAG, "Bluetooth turned ON")
onBluetoothStateChanged(BluetoothStatus.ENABLED)
bluetoothStatusManager.handleBluetoothStatus(BluetoothStatus.ENABLED)
}
BluetoothAdapter.STATE_OFF -> {
Log.d(TAG, "Bluetooth turned OFF")
onBluetoothStateChanged(BluetoothStatus.DISABLED)
bluetoothStatusManager.onBluetoothDisabled("User has turned off their Blue")
}
BluetoothAdapter.STATE_TURNING_ON, BluetoothAdapter.STATE_OFF -> {
Log.d(TAG, "Bluetooth state transitioning: ${bluetoothStatusManager.getAdapterStateName(intent.getIntExtra(
BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR))}")
}
}
}
}
//Register the receiver
val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
ContextCompat.registerReceiver(context, receiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
//Check initial Bluetooth state
val initialStatus = bluetoothStatusManager.checkBluetoothStatus()
onBluetoothStateChanged(initialStatus)
return receiver
}
}
/**