0

In the code below, "takePermission" and "bluetoothAdapter" aren't being recognized. I've been following along to this tutorial which seems slightly outdated, but his variables are recognized with no issue.

class MainActivity : ComponentActivity() {
    lateinit var bluetoothManager: BluetoothManager
    lateinit var bluetoothAdapter: BluetoothAdapter
    lateinit var takePermission: ActivityResultLauncher<String>
    lateinit var takeResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        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)
            } else {
                Toast.makeText(
                    applicationContext,
                    "Bluetooth Permission not given",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        // ...
    }
}

@Composable
fun Greeting() {
    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Text(
            text = "Bluetooth ON/OFF Application",
            fontSize = 20.sp,
            fontWeight = FontWeight.Bold,
            textAlign = TextAlign.Center,
        )
        Spacer(modifier = Modifier.height(20.dp))

        OutlinedButton(onClick = {
            takePermission.launch(android.Manifest.permission.BLUETOOTH_CONNECT) //unresolved reference error
        }) {
            Text(
                text = "Bluetooth On",
                fontSize = 30.sp,
                fontWeight = FontWeight.Bold,
            )
        }
        Spacer(modifier = Modifier.height(20.dp))

        OutlinedButton(onClick = {
            bluetoothAdapter.disable() //unresolved reference error
        }) {
            Text(
                text = "Bluetooth Off",
                fontSize = 30.sp,
                fontWeight = FontWeight.Bold,
            )
        }
        Spacer(modifier = Modifier.height(20.dp))
    }
}
3
  • You need to provide a little more info other than not being recognized. Have you tried running your code? Have you tried closing and reopening your IDE? Sometimes IDEs just glitch and don't reevaluate your code.
    – eliasciur
    Commented Jun 29 at 19:48
  • 1
    @eliasciur Running it just gives the unresolved reference error. Opening and closing doesn't do anything, either. I provided the relevant code and the error, what else are you looking for?
    – Incog
    Commented Jun 29 at 20:00
  • Can you post the actual error message the compiler is showing?
    – selbie
    Commented Jun 29 at 20:06

1 Answer 1

0

Shot in the dark guess based on observation of indent patterns. The indenting pattern suggests that Greeting isn't actually in your MainActivity's class scope. i.e. you have Greeting as a global function.

I suspect your code is structured like this:

class MainActivity : ComponentActivity() {
    lateinit var bluetoothManager: BluetoothManager
    lateinit var bluetoothAdapter: BluetoothAdapter
    lateinit var takePermission: ActivityResultLauncher<String>
    lateinit var takeResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
    }

    ...

}  // END OF MainActivity Defintition


// Greeting is at global scope
fun Greeting() {

}

You want:

class MainActivity : ComponentActivity() {
    lateinit var bluetoothManager: BluetoothManager
    lateinit var bluetoothAdapter: BluetoothAdapter
    lateinit var takePermission: ActivityResultLauncher<String>
    lateinit var takeResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
    }

    fun Greeting() {

    }

}  // END OF MainActivity Defintition

If this doesn't fix your issue, then I suspect you need to post an MVCE.

2
  • This was indeed the issue! That's how it was laid out in the new activity template, so I didn't even think to second guess something so simple. Thank you!
    – Incog
    Commented Jun 29 at 20:07
  • "That's how it was laid out in the new activity template" -- With good reason: Composables should generally be top-level functions. You should pass everything that a composable needs as a parameter and not rely on some class properties. That said, a composable should only handle simple objects, never something like a BluetoothAdapter. The idea is to pass a function as a parameter to the composable, like disableBluetooth: () -> Unit so your composable can simply call that. The caller can then provide the actual functionality.
    – Leviathan
    Commented Jun 29 at 23:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.