diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 7a65d8f..a415442 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -4,18 +4,18 @@ - + - + - - + + - - + + diff --git a/app/src/main/java/eu/m724/bt/MainActivity.kt b/app/src/main/java/eu/m724/bt/MainActivity.kt index ee6cdfe..03ed95f 100644 --- a/app/src/main/java/eu/m724/bt/MainActivity.kt +++ b/app/src/main/java/eu/m724/bt/MainActivity.kt @@ -1,18 +1,33 @@ package eu.m724.bt import android.Manifest +import android.annotation.SuppressLint +import android.app.Activity +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice import android.bluetooth.le.BluetoothLeScanner +import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle +import android.os.Handler import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.activity.result.ActivityResult +import androidx.activity.result.ActivityResultCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.ListItem import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.core.app.ActivityCompat @@ -27,12 +42,29 @@ class MainActivity : ComponentActivity() { onPermissionGranted(granted) } + private val btEnableActivityResult = + registerForActivityResult( + ActivityResultContracts.StartActivityForResult(), + ActivityResultCallback { result -> + if (result.resultCode != Activity.RESULT_OK) { + Toast.makeText( + applicationContext, + "you must enable bluetooth", + Toast.LENGTH_SHORT + ).show() + finishAffinity() + } + } + ) + + private val deviceList = mutableStateListOf("a", "b", "c") + private fun bluetoothPermission() { if (checkSelfPermission( - Manifest.permission.BLUETOOTH_SCAN + Manifest.permission.BLUETOOTH_CONNECT ) != PackageManager.PERMISSION_GRANTED ) { - requestPermission.launch(Manifest.permission.BLUETOOTH_SCAN) + requestPermission.launch(Manifest.permission.BLUETOOTH_CONNECT) } else onPermissionGranted(true) } @@ -40,7 +72,11 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - bluetoothHelper = BluetoothHelper(applicationContext) + bluetoothHelper = BluetoothHelper(applicationContext, deviceList) + Handler().postDelayed({ + System.out.println("aded") + deviceList.add("f") + }, 1000) setContent { PicoBtAndroidTheme { // A surface container using the 'background' color from the theme @@ -49,6 +85,13 @@ class MainActivity : ComponentActivity() { color = MaterialTheme.colorScheme.background ) { Greeting("Android") + LazyColumn ( + modifier = Modifier.fillMaxWidth() + ) { + items(deviceList) { item -> + Text(text = item) + } + } } } } @@ -61,11 +104,17 @@ class MainActivity : ComponentActivity() { } private fun onPermissionGranted(granted: Boolean) { - Toast.makeText( - applicationContext, - if (granted) "yes!!!!!!!!" else "no :(", - Toast.LENGTH_SHORT - ).show() + if (!granted) { + Toast.makeText( + applicationContext, + "you must grant bluetooth permission", + Toast.LENGTH_SHORT + ).show() + finishAffinity() + } else { + val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) + btEnableActivityResult.launch(enableBtIntent) + } } } diff --git a/app/src/main/java/eu/m724/bt/code/BluetoothHelper.kt b/app/src/main/java/eu/m724/bt/code/BluetoothHelper.kt index 06e1db3..1ccc840 100644 --- a/app/src/main/java/eu/m724/bt/code/BluetoothHelper.kt +++ b/app/src/main/java/eu/m724/bt/code/BluetoothHelper.kt @@ -1,6 +1,7 @@ package eu.m724.bt.code import android.Manifest +import android.annotation.SuppressLint import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothManager import android.bluetooth.le.BluetoothLeScanner @@ -8,44 +9,44 @@ import android.bluetooth.le.ScanCallback import android.bluetooth.le.ScanResult import android.content.Context import android.content.pm.PackageManager +import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.core.app.ActivityCompat +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class BluetoothHelper( - val context: Context + val context: Context, + val deviceList: SnapshotStateList ) { private val bluetoothManager: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter - private val bluetoothLeScanner: BluetoothLeScanner = - bluetoothAdapter.bluetoothLeScanner + private lateinit var bluetoothLeScanner: BluetoothLeScanner @Throws(SecurityException::class) - private fun init() { + fun init(): Int { + bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner + + if (!bluetoothAdapter.isEnabled) + return 2 bluetoothLeScanner.startScan(scanCallback) + System.out.println("scanning now") + return 0 } private fun onFailed(errorCode: Int) { } + @SuppressLint("MissingPermission") private fun addDevice(result: ScanResult) { - if (ActivityCompat.checkSelfPermission( - context, - Manifest.permission.BLUETOOTH_CONNECT - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return + println("Found: ${result.device.name} (${result.device.address})") + CoroutineScope(Dispatchers.Main).launch { + deviceList.add(result.device.name) } - println(result.device.name) } private val scanCallback: ScanCallback =