Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCsabaToth committed Jun 27, 2022
1 parent 1a7edaa commit 76aed89
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/src/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class FlutterBluePlus {
Stream<MethodCall> get _methodStream => _methodStreamController
.stream; // Used internally to dispatch methods from platform.

/// Fix for issue https://github.com/pauldemarco/flutter_blue/issues/608
/// Cached broadcast stream for FlutterBlue.state events
/// Caching this stream allows for more than one listener to subscribe
/// and unsubscribe apart from each other,
/// while allowing events to still be sent to others that are subscribed
Stream<BluetoothState>? _stateStream;

/// Singleton boilerplate
FlutterBluePlus._() {
_channel.setMethodCallHandler((MethodCall call) async {
Expand Down Expand Up @@ -83,10 +90,12 @@ class FlutterBluePlus {
.then((buffer) => protos.BluetoothState.fromBuffer(buffer))
.then((s) => BluetoothState.values[s.state.value]);

yield* _stateChannel
_stateStream ??= _stateChannel
.receiveBroadcastStream()
.map((buffer) => protos.BluetoothState.fromBuffer(buffer))
.map((s) => BluetoothState.values[s.state.value]);

yield* _stateStream!;
}

/// Retrieve a list of connected devices
Expand Down

0 comments on commit 76aed89

Please sign in to comment.