Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />

<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="30"/>

<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />

<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-feature android:name="android.hardware.bluetooth"/>

<uses-feature android:name="android.hardware.bluetooth"
android:required="true"/>

<application
android:name=".BluetoothApp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.bluetooth.BluetoothServerSocket
import android.bluetooth.BluetoothSocket
import android.content.Context
import android.content.IntentFilter
import android.os.Build
import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothController
import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothDeviceDomain
import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothMessage
Expand Down Expand Up @@ -83,7 +84,11 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro
}

override fun startDiscovery() {
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
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -71,6 +73,13 @@ class MainActivity : ComponentActivity() {
Manifest.permission.BLUETOOTH_CONNECT
)
)
} else {
permissionLauncher.launch(
arrayOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION
)
)
}

setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package dev.krishna.bluetoothchatapplication.presentation.components
import dev.krishna.bluetoothchatapplication.domain.chat.BluetoothMessage

object BluetoothSampleMessage {
val messages = listOf<BluetoothMessage>(getMessage(), getSecondMessage(), getMessage(), getSecondMessage())
val messages = listOf<BluetoothMessage>(
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}