Skip to content

Commit 3000063

Browse files
committed
add a function to pass through a stream of connection state information
1 parent 6207f50 commit 3000063

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/models/ergometer.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:c2bluetooth/constants.dart' as Identifiers;
1212
import 'package:flutter_ble_lib_ios_15/flutter_ble_lib.dart';
1313
import 'package:rxdart/rxdart.dart';
1414

15+
enum ErgometerConnectionState { connecting, connected, disconnected }
16+
1517
class Ergometer {
1618
Peripheral _peripheral;
1719
Csafe? _csafeClient;
@@ -58,6 +60,25 @@ class Ergometer {
5860
});
5961
}
6062

63+
/// Expose a stream of events to enable monitoring the erg's connection state
64+
/// This acts as a wrapper around the state provided by the internal bluetooth library to aid with swapping it out later.
65+
Stream<ErgometerConnectionState> monitorConnectionState() {
66+
return _peripheral.observeConnectionState().asyncMap((connectionState) {
67+
switch (connectionState) {
68+
case PeripheralConnectionState.connecting:
69+
return ErgometerConnectionState.connecting;
70+
case PeripheralConnectionState.connected:
71+
return ErgometerConnectionState.connected;
72+
case PeripheralConnectionState.disconnecting:
73+
return ErgometerConnectionState.disconnected;
74+
case PeripheralConnectionState.disconnected:
75+
return ErgometerConnectionState.disconnected;
76+
default:
77+
return ErgometerConnectionState.disconnected;
78+
}
79+
});
80+
}
81+
6182
/// A read function for the PM over bluetooth.
6283
///
6384
/// Intended for passing to the csafe_fitness library to allow it to read data from the erg

0 commit comments

Comments
 (0)