androidkotlinandroid-jetpack-composeandroid-bluetooth

Unresolved reference: Why isn't this variable being detected?


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))
    }
}

Solution

  • 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.