So in API 31 there are new Bluetooth permissions. I want to turn bluetooth on or off using this:
private void changeBluetoothState(boolean status) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (status)
mBluetoothAdapter.enable();
else mBluetoothAdapter.disable();
}
and in manifest I already have this:
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature
android:name="android.hardware.bluetooth"
android:required="false" />
Android documentations says to add android:maxSdkVersion="30" to the above bluetooth permissions if targeting API 31 or higher. I also get an error in Android Studio that the enable() and disable() functions need "android.permission.BLUETOOTH_CONNECT".
If adding
android:maxSdkVersion="30"to tell the system to ignore the statement on higher APIs is optional rather than obligatory, does that mean that not adding it will allow the statement to work on higher APIs?If
"android.permission.BLUETOOTH_CONNECT"is to allow my app to interact with other bluetooth devices then why is it needed to enable or disable the bluetooth adapter on the original device?If the BLUETOOTH_CONNECT permission needs to be requested at runtime what is the correct full way to do it? Meaning checking if it's already granted then requesting it if it's not. I have no Android 12 device so no way to test this code.