Commit 0596cba
Changed files (6)
app
app/lib/bluetooth/ble_read_cubit.dart
@@ -1,6 +1,6 @@
import 'dart:async';
-import 'package:blood_pressure_app/bluetooth/logging.dart';
+import 'package:blood_pressure_app/logging.dart';
import 'package:collection/collection.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
app/lib/bluetooth/bluetooth_cubit.dart
@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:blood_pressure_app/bluetooth/flutter_blue_plus_mockable.dart';
+import 'package:blood_pressure_app/logging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
@@ -35,44 +36,52 @@ class BluetoothCubit extends Cubit<BluetoothState> {
await super.close();
}
- void _onAdapterStateChanged(BluetoothAdapterState state) {
+ void _onAdapterStateChanged(BluetoothAdapterState state) async {
_adapterState = state;
switch (_adapterState) {
case BluetoothAdapterState.unavailable:
emit(BluetoothUnfeasible());
case BluetoothAdapterState.unauthorized:
emit(BluetoothUnauthorized());
+ await requestPermission();
case BluetoothAdapterState.on:
emit(BluetoothReady());
case BluetoothAdapterState.off:
case BluetoothAdapterState.turningOff:
case BluetoothAdapterState.turningOn:
emit(BluetoothDisabled());
+ await enableBluetooth();
case BluetoothAdapterState.unknown:
emit(BluetoothInitial());
}
+ }
- /// 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.');
- assert(await Permission.bluetoothConnect.isGranted, 'Permissions handler'
+ /// 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.');
+ try {
+ assert(!await Permission.bluetoothConnect.isGranted, 'Permissions handler'
'should report the same as blue_plus');
final permission = await Permission.bluetoothConnect.request();
return permission.isGranted;
+ } catch (error) {
+ Log.err('Failed to request bluetooth permissions', [error]);
+ return false;
}
- /// Request to enable bluetooth on the device
- Future<bool> enableBluetooth() async {
- assert(state is BluetoothDisabled, 'No need to enable bluetooth when '
- 'already enabled or not known to be disabled.');
+ }
+
+ /// Request to enable bluetooth on the device
+ Future<bool> enableBluetooth() async {
+ assert(state is BluetoothDisabled, 'No need to enable bluetooth when '
+ 'already enabled or not known to be disabled.');
+ try {
if (!Platform.isAndroid) return false;
- try {
- await _flutterBluePlus.turnOn();
- return true;
- } on FlutterBluePlusException {
- return false;
- }
+ await _flutterBluePlus.turnOn();
+ return true;
+ } on FlutterBluePlusException {
+ return false;
}
}
}
app/lib/bluetooth/device_scan_cubit.dart
@@ -2,7 +2,7 @@ import 'dart:async';
import 'package:blood_pressure_app/bluetooth/bluetooth_cubit.dart';
import 'package:blood_pressure_app/bluetooth/flutter_blue_plus_mockable.dart';
-import 'package:blood_pressure_app/bluetooth/logging.dart';
+import 'package:blood_pressure_app/logging.dart';
import 'package:blood_pressure_app/model/storage/settings_store.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
app/lib/bluetooth/logging.dart → app/lib/logging.dart
File renamed without changes
app/test/bluetooth/ble_read_cubit_test.dart
@@ -1,5 +1,5 @@
import 'package:blood_pressure_app/bluetooth/ble_read_cubit.dart';
-import 'package:blood_pressure_app/bluetooth/logging.dart';
+import 'package:blood_pressure_app/logging.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
app/test/bluetooth/bluetooth_cubit_test.dart
@@ -2,16 +2,21 @@ import 'dart:async';
import 'package:blood_pressure_app/bluetooth/bluetooth_cubit.dart';
import 'package:blood_pressure_app/bluetooth/flutter_blue_plus_mockable.dart';
+import 'package:blood_pressure_app/logging.dart';
+import 'package:flutter/widgets.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
-@GenerateNiceMocks([MockSpec<FlutterBluePlusMockable>()])
+@GenerateNiceMocks([
+ MockSpec<FlutterBluePlusMockable>(),
+])
import 'bluetooth_cubit_test.mocks.dart';
void main() {
test('should translate adapter stream to state', () async {
+ WidgetsFlutterBinding.ensureInitialized();
final bluePlus = MockFlutterBluePlusMockable();
when(bluePlus.adapterState).thenAnswer((_) =>
Stream.fromIterable([
@@ -23,6 +28,7 @@ void main() {
BluetoothAdapterState.turningOn,
BluetoothAdapterState.on,
]));
+ Log.testExpectError = true;
final cubit = BluetoothCubit(flutterBluePlus: bluePlus);
expect(cubit.state, isA<BluetoothInitial>());
@@ -35,8 +41,6 @@ void main() {
isA<BluetoothDisabled>(),
isA<BluetoothReady>(),
]));
+ Log.testExpectError = false;
});
- // TODO: integration tests ?
- test('should request permissions', () async {});
- test('should enable bluetooth', () async {});
}