ここには BLUETOOTH_CONNECT 権限を例にする。
権限ランチャーを作る
// 権限ランチャー
private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
// 権限を取得できた
Log.i("Permission: ", "Granted")
} else {
// 権限を取得できなかった
Log.i("Permission: ", "Denied")
}
}
権限をリクエスト
private fun requestBluetoothPermission() {
when {
ContextCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH_CONNECT
) == PackageManager.PERMISSION_GRANTED -> {
bluetoothInit()
}
ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.BLUETOOTH_CONNECT
) -> {
Toast.makeText(this, "Bluetooth 権限は必要にゃ。", Toast.LENGTH_LONG).show()
requestPermissionLauncher.launch(
Manifest.permission.BLUETOOTH_CONNECT
)
}
else -> {
Toast.makeText(this, "Bluetooth 権限は必要にゃ。", Toast.LENGTH_LONG).show()
requestPermissionLauncher.launch(
Manifest.permission.BLUETOOTH_CONNECT
)
}
}
}