mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 08:05:22 +00:00
bluetooth check
This commit is contained in:
@@ -158,8 +158,17 @@ class MainActivity : ComponentActivity() {
|
|||||||
*/
|
*/
|
||||||
private fun checkBluetoothAndProceed() {
|
private fun checkBluetoothAndProceed() {
|
||||||
android.util.Log.d("MainActivity", "Checking Bluetooth status")
|
android.util.Log.d("MainActivity", "Checking Bluetooth status")
|
||||||
bluetoothStatusManager.logBluetoothStatus()
|
|
||||||
|
|
||||||
|
// For first-time users, skip Bluetooth check and go straight to permissions
|
||||||
|
// We'll check Bluetooth after permissions are granted
|
||||||
|
if (permissionManager.isFirstTimeLaunch()) {
|
||||||
|
android.util.Log.d("MainActivity", "First-time launch, skipping Bluetooth check - will check after permissions")
|
||||||
|
proceedWithPermissionCheck()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For existing users, check Bluetooth status first
|
||||||
|
bluetoothStatusManager.logBluetoothStatus()
|
||||||
bluetoothStatus = bluetoothStatusManager.checkBluetoothStatus()
|
bluetoothStatus = bluetoothStatusManager.checkBluetoothStatus()
|
||||||
|
|
||||||
when (bluetoothStatus) {
|
when (bluetoothStatus) {
|
||||||
@@ -168,7 +177,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
proceedWithPermissionCheck()
|
proceedWithPermissionCheck()
|
||||||
}
|
}
|
||||||
BluetoothStatus.DISABLED -> {
|
BluetoothStatus.DISABLED -> {
|
||||||
// Show Bluetooth enable screen
|
// Show Bluetooth enable screen (should have permissions as existing user)
|
||||||
android.util.Log.d("MainActivity", "Bluetooth disabled, showing enable screen")
|
android.util.Log.d("MainActivity", "Bluetooth disabled, showing enable screen")
|
||||||
onboardingState = OnboardingState.BLUETOOTH_CHECK
|
onboardingState = OnboardingState.BLUETOOTH_CHECK
|
||||||
isBluetoothLoading = false
|
isBluetoothLoading = false
|
||||||
@@ -183,10 +192,10 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proceed with permission checking after Bluetooth is confirmed enabled
|
* Proceed with permission checking
|
||||||
*/
|
*/
|
||||||
private fun proceedWithPermissionCheck() {
|
private fun proceedWithPermissionCheck() {
|
||||||
android.util.Log.d("MainActivity", "Bluetooth enabled, checking permissions")
|
android.util.Log.d("MainActivity", "Proceeding with permission check")
|
||||||
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
delay(200) // Small delay for smooth transition
|
delay(200) // Small delay for smooth transition
|
||||||
@@ -223,20 +232,46 @@ class MainActivity : ComponentActivity() {
|
|||||||
isBluetoothLoading = false
|
isBluetoothLoading = false
|
||||||
bluetoothStatus = bluetoothStatusManager.checkBluetoothStatus()
|
bluetoothStatus = bluetoothStatusManager.checkBluetoothStatus()
|
||||||
|
|
||||||
if (bluetoothStatus == BluetoothStatus.NOT_SUPPORTED) {
|
when {
|
||||||
// Show permanent error for unsupported devices
|
bluetoothStatus == BluetoothStatus.NOT_SUPPORTED -> {
|
||||||
errorMessage = message
|
// Show permanent error for unsupported devices
|
||||||
onboardingState = OnboardingState.ERROR
|
errorMessage = message
|
||||||
} else {
|
onboardingState = OnboardingState.ERROR
|
||||||
// Stay on Bluetooth check screen for retry
|
}
|
||||||
onboardingState = OnboardingState.BLUETOOTH_CHECK
|
message.contains("Permission") && permissionManager.isFirstTimeLaunch() -> {
|
||||||
|
// During first-time onboarding, if Bluetooth enable fails due to permissions,
|
||||||
|
// proceed to permission explanation screen where user will grant permissions first
|
||||||
|
android.util.Log.d("MainActivity", "Bluetooth enable requires permissions, proceeding to permission explanation")
|
||||||
|
proceedWithPermissionCheck()
|
||||||
|
}
|
||||||
|
message.contains("Permission") -> {
|
||||||
|
// For existing users, redirect to permission explanation to grant missing permissions
|
||||||
|
android.util.Log.d("MainActivity", "Bluetooth enable requires permissions, showing permission explanation")
|
||||||
|
onboardingState = OnboardingState.PERMISSION_EXPLANATION
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
// Stay on Bluetooth check screen for retry
|
||||||
|
onboardingState = OnboardingState.BLUETOOTH_CHECK
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleOnboardingComplete() {
|
private fun handleOnboardingComplete() {
|
||||||
android.util.Log.d("MainActivity", "Onboarding completed, initializing app")
|
android.util.Log.d("MainActivity", "Onboarding completed, checking Bluetooth again before initializing app")
|
||||||
onboardingState = OnboardingState.INITIALIZING
|
|
||||||
initializeApp()
|
// After permissions are granted, re-check Bluetooth status
|
||||||
|
val currentBluetoothStatus = bluetoothStatusManager.checkBluetoothStatus()
|
||||||
|
if (currentBluetoothStatus == BluetoothStatus.ENABLED) {
|
||||||
|
// Bluetooth is enabled, proceed to app initialization
|
||||||
|
onboardingState = OnboardingState.INITIALIZING
|
||||||
|
initializeApp()
|
||||||
|
} else {
|
||||||
|
// Bluetooth still disabled, but now we have permissions to enable it
|
||||||
|
android.util.Log.d("MainActivity", "Permissions granted, but Bluetooth still disabled. Showing Bluetooth enable screen.")
|
||||||
|
bluetoothStatus = currentBluetoothStatus
|
||||||
|
onboardingState = OnboardingState.BLUETOOTH_CHECK
|
||||||
|
isBluetoothLoading = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleOnboardingFailed(message: String) {
|
private fun handleOnboardingFailed(message: String) {
|
||||||
|
|||||||
@@ -71,14 +71,23 @@ class BluetoothStatusManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if Bluetooth is currently enabled
|
* Check if Bluetooth is currently enabled (permission-safe)
|
||||||
*/
|
*/
|
||||||
fun isBluetoothEnabled(): Boolean {
|
fun isBluetoothEnabled(): Boolean {
|
||||||
return bluetoothAdapter?.isEnabled == true
|
return try {
|
||||||
|
bluetoothAdapter?.isEnabled == true
|
||||||
|
} catch (securityException: SecurityException) {
|
||||||
|
// If we can't check due to permissions, assume disabled
|
||||||
|
Log.w(TAG, "Cannot check Bluetooth enabled state due to missing permissions")
|
||||||
|
false
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Error checking Bluetooth enabled state: ${e.message}")
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check Bluetooth status and handle accordingly
|
* Check Bluetooth status and handle accordingly (permission-safe)
|
||||||
* This should be called on every app startup
|
* This should be called on every app startup
|
||||||
*/
|
*/
|
||||||
fun checkBluetoothStatus(): BluetoothStatus {
|
fun checkBluetoothStatus(): BluetoothStatus {
|
||||||
@@ -89,8 +98,8 @@ class BluetoothStatusManager(
|
|||||||
Log.e(TAG, "Bluetooth not supported on this device")
|
Log.e(TAG, "Bluetooth not supported on this device")
|
||||||
BluetoothStatus.NOT_SUPPORTED
|
BluetoothStatus.NOT_SUPPORTED
|
||||||
}
|
}
|
||||||
!bluetoothAdapter!!.isEnabled -> {
|
!isBluetoothEnabled() -> {
|
||||||
Log.w(TAG, "Bluetooth is disabled")
|
Log.w(TAG, "Bluetooth is disabled or cannot be checked")
|
||||||
BluetoothStatus.DISABLED
|
BluetoothStatus.DISABLED
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
@@ -101,7 +110,7 @@ class BluetoothStatusManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request user to enable Bluetooth
|
* Request user to enable Bluetooth (permission-aware)
|
||||||
*/
|
*/
|
||||||
fun requestEnableBluetooth() {
|
fun requestEnableBluetooth() {
|
||||||
Log.d(TAG, "Requesting user to enable Bluetooth")
|
Log.d(TAG, "Requesting user to enable Bluetooth")
|
||||||
@@ -109,6 +118,10 @@ class BluetoothStatusManager(
|
|||||||
try {
|
try {
|
||||||
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||||
bluetoothEnableLauncher?.launch(enableBluetoothIntent)
|
bluetoothEnableLauncher?.launch(enableBluetoothIntent)
|
||||||
|
} catch (securityException: SecurityException) {
|
||||||
|
// Permission not granted yet - this is expected during onboarding
|
||||||
|
Log.w(TAG, "Cannot request Bluetooth enable due to missing BLUETOOTH_CONNECT permission")
|
||||||
|
onBluetoothDisabled("Bluetooth permissions are required before enabling Bluetooth. Please grant permissions first.")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to request Bluetooth enable", e)
|
Log.e(TAG, "Failed to request Bluetooth enable", e)
|
||||||
onBluetoothDisabled("Failed to request Bluetooth enable: ${e.message}")
|
onBluetoothDisabled("Failed to request Bluetooth enable: ${e.message}")
|
||||||
@@ -147,7 +160,7 @@ class BluetoothStatusManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get detailed diagnostics
|
* Get detailed diagnostics (permission-safe)
|
||||||
*/
|
*/
|
||||||
fun getDiagnostics(): String {
|
fun getDiagnostics(): String {
|
||||||
return buildString {
|
return buildString {
|
||||||
@@ -157,9 +170,18 @@ class BluetoothStatusManager(
|
|||||||
appendLine("Bluetooth enabled: ${isBluetoothEnabled()}")
|
appendLine("Bluetooth enabled: ${isBluetoothEnabled()}")
|
||||||
appendLine("Current status: ${checkBluetoothStatus()}")
|
appendLine("Current status: ${checkBluetoothStatus()}")
|
||||||
|
|
||||||
|
// Only access adapter details if we have permission and adapter is available
|
||||||
bluetoothAdapter?.let { adapter ->
|
bluetoothAdapter?.let { adapter ->
|
||||||
appendLine("Adapter name: ${adapter.name ?: "Unknown"}")
|
try {
|
||||||
appendLine("Adapter address: ${adapter.address ?: "Unknown"}")
|
// These calls require BLUETOOTH_CONNECT permission on Android 12+
|
||||||
|
appendLine("Adapter name: ${adapter.name ?: "Unknown"}")
|
||||||
|
appendLine("Adapter address: ${adapter.address ?: "Unknown"}")
|
||||||
|
} catch (securityException: SecurityException) {
|
||||||
|
// Permission not granted yet, skip detailed info
|
||||||
|
appendLine("Adapter details: [Permission required]")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
appendLine("Adapter details: [Error: ${e.message}]")
|
||||||
|
}
|
||||||
appendLine("Adapter state: ${getAdapterStateName(adapter.state)}")
|
appendLine("Adapter state: ${getAdapterStateName(adapter.state)}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user