From b16c8e2d7e1bef56e34fef409c9c216ddf81a945 Mon Sep 17 00:00:00 2001 From: Srikrishna Sakunia Date: Sat, 10 Jun 2023 18:07:14 +0530 Subject: [PATCH] [Push Number]: 6. [Description]: Fixed the connectivity issue due which Devices below Android S were not able to request and run bluetooth and fixed the text message logic. --- app/src/main/AndroidManifest.xml | 22 ++++++++++-- .../data/chat/AndroidBluetoothController.kt | 35 +++++++++++++++---- .../data/chat/BluetoothMessageMapper.kt | 2 +- .../presentation/MainActivity.kt | 11 +++++- .../components/BluetoothSampleMessage.kt | 14 +++++--- .../presentation/components/ChatScreen.kt | 1 - .../utils/UtilFunctions.kt | 5 ++- 7 files changed, 72 insertions(+), 18 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 317ee2f..2c6af74 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,11 +2,27 @@ - - + + + + + + + - + + = Build.VERSION_CODES.S) { + if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + return + } + }else if (!hasPermission(context, Manifest.permission.BLUETOOTH)) { return } @@ -96,8 +101,13 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro } override fun stopDiscovery() { - if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + return + } + }else if (!hasPermission(context, Manifest.permission.BLUETOOTH)) { return + } bluetoothAdapter?.cancelDiscovery() } @@ -177,8 +187,13 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro } override suspend fun sendMessage(message: String): BluetoothMessage? { - if ((!hasPermission(context,Manifest.permission.BLUETOOTH_CONNECT)) || - (bluetoothDataTransferService == null)){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT) + || (bluetoothDataTransferService == null)) { + return null + } + } else if (!hasPermission(context, Manifest.permission.BLUETOOTH) || + (bluetoothDataTransferService == null)) { return null } val bluetoothMessage = BluetoothMessage( @@ -206,7 +221,11 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro } private fun updatePairedDevices() { - if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + return + } + } else if (!hasPermission(context,Manifest.permission.BLUETOOTH)){ return } bluetoothAdapter @@ -219,7 +238,11 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro } private fun checkBluetoothPermission() { - if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (!hasPermission(context, Manifest.permission.BLUETOOTH_CONNECT)) { + throw SecurityException(BLUETOOTH_PERMISSION_ERROR_MESSAGE) + } + }else if (!hasPermission(context, Manifest.permission.BLUETOOTH)) { throw SecurityException(BLUETOOTH_PERMISSION_ERROR_MESSAGE) } } diff --git a/app/src/main/java/dev/krishna/bluetoothchatapplication/data/chat/BluetoothMessageMapper.kt b/app/src/main/java/dev/krishna/bluetoothchatapplication/data/chat/BluetoothMessageMapper.kt index 3af71ca..d8c8bc7 100644 --- a/app/src/main/java/dev/krishna/bluetoothchatapplication/data/chat/BluetoothMessageMapper.kt +++ b/app/src/main/java/dev/krishna/bluetoothchatapplication/data/chat/BluetoothMessageMapper.kt @@ -6,7 +6,7 @@ import java.util.Date fun String.toBluetoothMessage(isFromLocalSender: Boolean): BluetoothMessage{ val name = substringBefore(" # ") - val message = substringAfter(" # ") + val message = substringAfter(" # ").substringBeforeLast(" # ") val timestamp = substringAfterLast(" # ") return BluetoothMessage( message = message, diff --git a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/MainActivity.kt b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/MainActivity.kt index 78a6ee9..575600b 100644 --- a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/MainActivity.kt +++ b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/MainActivity.kt @@ -55,7 +55,9 @@ class MainActivity : ComponentActivity() { ){ perms-> val canEnableBluetooth = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ){ perms[Manifest.permission.BLUETOOTH_CONNECT] == true - } else true + } else { + perms[Manifest.permission.BLUETOOTH] == true && perms[Manifest.permission.ACCESS_COARSE_LOCATION] == true + } if (canEnableBluetooth && !isBluetoothEnabled){ enableBluetoothLauncher.launch( @@ -71,6 +73,13 @@ class MainActivity : ComponentActivity() { Manifest.permission.BLUETOOTH_CONNECT ) ) + } else { + permissionLauncher.launch( + arrayOf( + Manifest.permission.BLUETOOTH, + Manifest.permission.ACCESS_COARSE_LOCATION + ) + ) } setContent { diff --git a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/BluetoothSampleMessage.kt b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/BluetoothSampleMessage.kt index 28bbb11..f525ff7 100644 --- a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/BluetoothSampleMessage.kt +++ b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/BluetoothSampleMessage.kt @@ -3,20 +3,24 @@ package dev.krishna.bluetoothchatapplication.presentation.components import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothMessage object BluetoothSampleMessage { - val messages = listOf(getMessage(), getSecondMessage(), getMessage(), getSecondMessage()) + val messages = listOf( + getMessage(1), getSecondMessage(2), getMessage(3), getSecondMessage(4), + getMessage(5), getSecondMessage(6), getMessage(7), getSecondMessage(8), + getMessage(9), getSecondMessage(10), getMessage(11), getSecondMessage(12), + getMessage(13), getSecondMessage(14), getMessage(15), getSecondMessage(16)) - fun getMessage(): BluetoothMessage{ + fun getMessage(no: Int): BluetoothMessage{ return BluetoothMessage( - "Hello There", + "Hello There $no", "Krishna", true, "1685902490408" ) } - fun getSecondMessage(): BluetoothMessage{ + fun getSecondMessage(no: Int): BluetoothMessage{ return BluetoothMessage( - "Hi", + "Hi $no", "lavanya", false, "1685902490408" diff --git a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/ChatScreen.kt b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/ChatScreen.kt index 7e6479a..157dae4 100644 --- a/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/ChatScreen.kt +++ b/app/src/main/java/dev/krishna/bluetoothchatapplication/presentation/components/ChatScreen.kt @@ -25,7 +25,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothMessage import dev.krishna.bluetoothchatapplication.presentation.BluetoothUiState import dev.krishna.bluetoothchatapplication.ui.theme.BluetoothChatApplicationTheme diff --git a/app/src/main/java/dev/krishna/bluetoothchatapplication/utils/UtilFunctions.kt b/app/src/main/java/dev/krishna/bluetoothchatapplication/utils/UtilFunctions.kt index b0815e9..5b8d324 100644 --- a/app/src/main/java/dev/krishna/bluetoothchatapplication/utils/UtilFunctions.kt +++ b/app/src/main/java/dev/krishna/bluetoothchatapplication/utils/UtilFunctions.kt @@ -2,7 +2,10 @@ package dev.krishna.bluetoothchatapplication.utils import android.content.Context import android.content.pm.PackageManager +import android.util.Log fun hasPermission(context: Context,permission: String): Boolean{ - return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED + val isPermissionGranted = context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED + Log.d("Permission Handler","$permission granted = $isPermissionGranted") + return isPermissionGranted } \ No newline at end of file