From f8b7e24f2a1abdf6dd5c04397801715633e73372 Mon Sep 17 00:00:00 2001 From: ansonYam Date: Wed, 4 Jan 2023 15:57:56 -0800 Subject: [PATCH] Replace the deprecated 'startActivityForResult' --- .../android/bluetoothle/kotlin/MainActivity.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt index c79aae135..1ebf266b7 100644 --- a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt +++ b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt @@ -28,8 +28,19 @@ class MainActivity : AppCompatActivity() { // displays a dialog requesting user permission to enable Bluetooth. if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) { val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) - startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT) + getResult.launch(enableBtIntent) } // [END enable_bluetooth] } + + private val getResult = + registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { + if (it.resultCode == Activity.RESULT_OK) { + // granted + } else { + // denied + } + } }