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