I just added to the manifest:
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
and then I requested those permissions from Main Activity as any other.
For requesting permission I am using library
implementation 'pub.devrel:easypermissions:3.0.0'
then you can just call this function
public static final String[] BLUETOOTH_PERMISSIONS_S = { Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT} ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!EasyPermissions.hasPermissions(this, BLUETOOTH_PERMISSIONS_S)) {
EasyPermissions.requestPermissions(this, message, yourRequestCode,BLUETOOTH_PERMISSIONS_S);
}
}
and override onRequestPermissionResult
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
UPDATE FOR JETPACK COMPOSE
If you are using jetpack compose you can handle it like this:
Create a list of your permissions inside of rememberMultiplePermissionState function
rememberMultiplePermissionsState(
permissions = listOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)
)
Then observe a lifecycle events and on resume launch permission request
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(key1 = lifecycleOwner,
effect = {
val observer = LifecycleEventObserver { _, event ->
if(event == Lifecycle.Event.ON_START) {
permissionsState.launchMultiplePermissionRequest()
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
})
Observe the permission state
permissionsState.permissions.forEach { permissionState ->
when(permissionState.permission) {
Manifest.permission.ACCESS_FINE_LOCATION -> {
when {
permissionState.hasPermission -> {}
}
}
}
}
}