mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 18:25:21 +00:00
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:
@@ -144,18 +144,20 @@ private fun BluetoothDisabledContent(
|
||||
)
|
||||
}
|
||||
|
||||
OutlinedButton(
|
||||
onClick = onRetry,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text(
|
||||
text = "Check Again",
|
||||
style = MaterialTheme.typography.bodyMedium.copy(
|
||||
fontFamily = FontFamily.Monospace
|
||||
),
|
||||
modifier = Modifier.padding(vertical = 4.dp)
|
||||
)
|
||||
}
|
||||
//Since we are automatically checking bluetooth state -- commented
|
||||
|
||||
// OutlinedButton(
|
||||
// onClick = onRetry,
|
||||
// modifier = Modifier.fillMaxWidth()
|
||||
// ) {
|
||||
// Text(
|
||||
// text = "Check Again",
|
||||
// style = MaterialTheme.typography.bodyMedium.copy(
|
||||
// fontFamily = FontFamily.Monospace
|
||||
// ),
|
||||
// modifier = Modifier.padding(vertical = 4.dp)
|
||||
// )
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user