'How can we use a late init var declared in Component Actvity, inside Composable functions placed in a diferent kotlin file?

I am building an app which needs the user to turn ON and OFF the bluetooth of the phone.To get permissions and run the bluetooth system service I have delcared the permissions and activityresult as,

class BluetoothPermission : ComponentActivity() {
    lateinit var bluetoothManager: BluetoothManager
    lateinit var bluetoothAdapter: BluetoothAdapter
    lateinit var takepermission: ActivityResultLauncher<String>
    lateinit var takeresultlauncher: ActivityResultLauncher<Intent>
    override fun onCreate(savedInstanceState: Bundle?) {


        bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
        bluetoothAdapter = bluetoothManager.adapter
        takepermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (it) {
                val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                takeresultlauncher.launch(intent)

File Structure

File Structure

  • How can I access the takerpermission variable to another kotlin file which holds a button inside Devices.kt in a composable, if the BluetoothPermission(User created Activity) is already running.

  • What is the best Practice for acessing variables or should just make another activity and call it ?

  • The reason I don't want to make another activity is due to fact the UI doesn't change.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source