Skip to content

Commit

Permalink
chore: return nullable boolean from manager.enable to prevent closed_…
Browse files Browse the repository at this point in the history
…bluetooth_input from openening appsettings on non android devices

chore: improve some logs/formatting
  • Loading branch information
pimlie committed Oct 16, 2024
1 parent d9ea3ca commit 41d9aef
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
37 changes: 24 additions & 13 deletions app/lib/features/bluetooth/backend/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ abstract class BluetoothDevice<
}
}, onError: onError);

await backendConnect();
try {
await backendConnect();
} catch (e) {
logger.severe('Failed to connect to device', e);
if (!completer.isCompleted) completer.complete(false);
}

return completer.future.then((res) {
logger.finer('connect: completer.resolved($res)');
return res;
Expand Down Expand Up @@ -317,17 +323,18 @@ mixin CharacteristicValueListener<

disconnectCallbacks.add(disconnectCallback);

final listener = characteristicValueStream.listen((value) {
if (value == null) {
// ignore null values
return;
}
final listener = characteristicValueStream.listen(
(value) {
if (value == null) {
// ignore null values
return;
}

logger.finer('listenCharacteristicValue[${value.length}] $value');
logger.finer('listenCharacteristicValue[${value.length}] $value');

receivedSomeData = true;
onValue(value, completer.complete);
},
receivedSomeData = true;
onValue(value, completer.complete);
},
cancelOnError: true,
onDone: () {
logger.finer('listenCharacteristicValue: onDone called');
Expand All @@ -345,9 +352,13 @@ mixin CharacteristicValueListener<
_readCharacteristicCompleters.add(completer);
_readCharacteristicListeners.add(listener);

final bool triggerSuccess = await triggerCharacteristicValue(characteristic);
if (!triggerSuccess) {
logger.warning('listenCharacteristicValue: triggerCharacteristicValue returned $triggerSuccess');
try {
final bool triggerSuccess = await triggerCharacteristicValue(characteristic);
if (!triggerSuccess) {
logger.warning('listenCharacteristicValue: triggerCharacteristicValue returned $triggerSuccess');
}
} catch (e) {
logger.severe('Error occured while triggering characteristic', e);
}

return completer.future.then((res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ final class BluetoothLowEnergyManager extends BluetoothManager<DiscoveredEventAr
}

@override
Future<bool> enable() async => Platform.isAndroid && await backend.authorize();
Future<bool?> enable() async {
if (!Platform.isAndroid) {
return null;
}

final b = await backend.authorize();
await backend.showAppSettings();
return b;
}

/// The actual backend implementation
final CentralManager backend = CentralManager();
Expand Down
2 changes: 1 addition & 1 deletion app/lib/features/bluetooth/backend/bluetooth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class BluetoothManager<BackendDevice, BackendUuid, BackendService, Back
///
/// Returns null if no permissions were requested (ie because its not needed on a platform)
/// or true/false to indicate whether requesting permissions succeeded (not if it was granted)
Future<bool> enable(); // TODO: use task specific plugin/native code
Future<bool?> enable(); // TODO: use task specific plugin/native code

/// Last known adapter state
///
Expand Down
1 change: 0 additions & 1 deletion app/lib/features/bluetooth/backend/bluetooth_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:blood_pressure_app/logging.dart';
import 'package:collection/collection.dart';

/// Bluetooth Base UUID from Bluetooth Core Spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class FlutterBluePlusManager extends BluetoothManager<ScanResult, Guid, fbp.Blue
final FlutterBluePlusMockable backend;

@override
Future<bool> enable() async {
Future<bool?> enable() async {
if (Platform.isAndroid) {
await backend.turnOn();
return true;
}
return false;
return null;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion app/lib/features/bluetooth/backend/mock/mock_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class MockBluetoothManager extends BluetoothManager<String, String, Mocked
BluetoothDeviceDiscovery<BluetoothManager> get discovery => throw UnimplementedError();

@override
Future<bool> enable() async => false;
Future<bool?> enable() async => null;

@override
BluetoothAdapterState get lastKnownAdapterState => BluetoothAdapterState.initial;
Expand Down
4 changes: 2 additions & 2 deletions app/lib/features/bluetooth/logic/bluetooth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BluetoothCubit extends Cubit<BluetoothState> with TypeLogger {
void _onAdapterStateChanged(BluetoothAdapterState state) async {
if (state == BluetoothAdapterState.unauthorized) {
final success = await manager.enable();
if (!success) {
if (success != true) {
logger.warning('Enabling bluetooth failed or not needed on this platform');
}
}
Expand All @@ -47,7 +47,7 @@ class BluetoothCubit extends Cubit<BluetoothState> with TypeLogger {
}

/// Request to enable bluetooth on the device
Future<bool> enableBluetooth() async {
Future<bool?> enableBluetooth() async {
assert(state is BluetoothStateDisabled, 'No need to enable bluetooth when '
'already enabled or not known to be disabled.');
try {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/features/bluetooth/ui/closed_bluetooth_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ClosedBluetoothInput extends StatelessWidget with TypeLogger {
icon: Icons.bluetooth_disabled,
onTap: () async {
final bluetoothOn = await bluetoothCubit.enableBluetooth();
if (!bluetoothOn) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
if (bluetoothOn == false) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
bluetoothCubit.forceRefresh();
},
),
Expand Down

0 comments on commit 41d9aef

Please sign in to comment.