Commit 61c5d0a
Changed files (3)
app
android
app
src
lib
app/android/app/src/main/AndroidManifest.xml
@@ -13,13 +13,13 @@
<!-- New Bluetooth permissions in Android 12
https://developer.android.com/about/versions/12/features/bluetooth-permissions -->
- <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>
<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
app/lib/bluetooth/bluetooth_cubit.dart
@@ -45,14 +45,13 @@ class BluetoothCubit extends Cubit<BluetoothState> {
emit(BluetoothUnauthorized());
await requestPermission();
case BluetoothAdapterState.on:
- if (await Permission.bluetoothConnect.isGranted) {
+ if (await requestPermission()) {
emit(BluetoothReady());
Permission.bluetoothConnect
- .onGrantedCallback(() => _onAdapterStateChanged(state));
+ .onDeniedCallback(() => _onAdapterStateChanged(state));
} else {
emit(BluetoothUnauthorized());
- }
-
+ }sta
case BluetoothAdapterState.off:
case BluetoothAdapterState.turningOff:
case BluetoothAdapterState.turningOn:
@@ -65,13 +64,27 @@ class BluetoothCubit extends Cubit<BluetoothState> {
/// Request the permission to connect to bluetooth devices.
Future<bool> requestPermission() async {
- assert(_adapterState == BluetoothAdapterState.unauthorized, 'No need to '
- 'request permission when device unavailable or already authorized.');
+ // Permissions are not only required for connecting, but sometimes also for
+ // reading values.
try {
- assert(!await Permission.bluetoothConnect.isGranted, 'Permissions handler'
- 'should report the same as blue_plus');
- final permission = await Permission.bluetoothConnect.request();
- return permission.isGranted;
+ bool connectPermission = await Permission.bluetoothConnect.isGranted;
+ bool locationPermission = await Permission.locationWhenInUse.isGranted;
+ bool bluetoothPermission = await Permission.bluetooth.isGranted;
+ if (!connectPermission) {
+ connectPermission = await Permission.bluetoothConnect.request().isGranted;
+ Log.trace('requestPermission: connectPermission = $connectPermission');
+ }
+ if (!locationPermission) {
+ locationPermission = await Permission.locationWhenInUse.request().isGranted;
+ Log.trace('requestPermission: locationPermission = $locationPermission');
+ }
+ if (!bluetoothPermission) {
+ bluetoothPermission = await Permission.bluetooth.request().isGranted;
+ Log.trace('requestPermission: bluetoothPermission = $bluetoothPermission');
+ }
+ return connectPermission
+ && bluetoothPermission
+ && locationPermission;
} catch (error) {
Log.err('Failed to request bluetooth permissions', [error]);
return false;
app/lib/bluetooth/device_scan_cubit.dart
@@ -65,7 +65,7 @@ class DeviceScanCubit extends Cubit<DeviceScanState> {
// Not all devices are found using this configuration (https://pub.dev/packages/flutter_blue_plus#scanning-does-not-find-my-device).
// As long as no significant issues arise from this these devices are
// considered unsupported.
-
+ androidUsesFineLocation: true,
);
} catch (e) {
_onScanError(e);