Skip to content

Commit

Permalink
Fix Bluetooth permission checker in StartView
Browse files Browse the repository at this point in the history
  • Loading branch information
holmmi committed May 3, 2022
1 parent 0dd5a11 commit c8b6d17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
40 changes: 18 additions & 22 deletions app/src/main/java/fi/metropolia/movesense/util/PermissionUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class PermissionUtil {
context: Context,
onCheckPermissions: (Array<String>) -> Unit
): Boolean {
var btChecked = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(
context,
Expand All @@ -29,32 +28,29 @@ class PermissionUtil {
Manifest.permission.BLUETOOTH_CONNECT
)
)
} else {
btChecked = true
return false
}
return true
} else {
btChecked = true
}
var locChecked = false
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
onCheckPermissions(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
onCheckPermissions(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
)
)
} else {
locChecked = true
return false
}
return true
}
return btChecked && locChecked
}
}
}
17 changes: 5 additions & 12 deletions app/src/main/java/fi/metropolia/movesense/view/start/StartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun StartView(navController: NavController, startViewModel: StartViewModel = vie
val isSearching = startViewModel.isSearching.observeAsState()
val permissionsLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
it.values.all { value -> value }
permissionsGiven = it.values.all { value -> value }
}

Scaffold(
Expand Down Expand Up @@ -59,22 +59,15 @@ fun StartView(navController: NavController, startViewModel: StartViewModel = vie
isSearching = isSearching.value ?: false,
onStartScan = { startViewModel.startScan() }
)
} else {
permissionsGiven =
PermissionUtil.checkBluetoothPermissions(
context,
onCheckPermissions = { permissionsLauncher.launch(it) }
)
}
}
}
)

LaunchedEffect(Unit) {
permissionsGiven =
PermissionUtil.checkBluetoothPermissions(
context,
onCheckPermissions = { permissionsLauncher.launch(it) }
)
permissionsGiven = PermissionUtil.checkBluetoothPermissions(
context,
onCheckPermissions = { permissionsLauncher.launch(it) }
)
}
}

0 comments on commit c8b6d17

Please sign in to comment.