В приведенном ниже коде «takePermission» и «bluetoothAdapter» не распознаются. Я следовал этому руководству, которое кажется немного устаревшим, но его переменные распознаются без проблем.
class MainActivity : ComponentActivity() {
lateinit var bluetoothManager: BluetoothManager
lateinit var bluetoothAdapter: BluetoothAdapter
lateinit var takePermission: ActivityResultLauncher
lateinit var takeResultLauncher: ActivityResultLauncher
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("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("Bluetooth On", fontSize = 30.sp,
fontWeight = FontWeight.Bold)
}
Spacer(modifier = Modifier.height(20.dp))
OutlinedButton(onClick = {
bluetoothAdapter.disable() //unresolved reference error
}) {
Text("Bluetooth Off", fontSize = 30.sp,
fontWeight = FontWeight.Bold)
}
Spacer(modifier = Modifier.height(20.dp))
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... dio-kotlin
Почему эта переменная не обнаруживается? Android-студия Котлин ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
В чем моя проблема с расшифровкой этого файла? (Котлин/Android-студия)
Anonymous » » в форуме Android - 0 Ответы
- 21 Просмотры
-
Последнее сообщение Anonymous
-