Skip to content

Commit a6dd926

Browse files
Fixes subscription to _deviceConnector.deviceConnectionStateUpdateStream leaking (#876)
- Previous code would create a broadcastStream from a normal stream (created by Repeater). When this is done, the source stream subscription needs to be explicitly closed. This is usually done by implementing the onCancel callback from the asBroadcastStream method. This was not happening, so every call to connectedDeviceStream was creating a new subscription that was never closed. - To fix the problem, this commit uses the Repeater.broadcast constructor which already returns a broadcast stream and also closes the source stream subscription when needed
1 parent 96e083a commit a6dd926

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

packages/flutter_reactive_ble/lib/src/reactive_ble.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ class FlutterReactiveBle {
6060
BleStatus get status => _status;
6161

6262
/// A stream providing connection updates for all the connected BLE devices.
63-
Stream<ConnectionStateUpdate> get connectedDeviceStream => Repeater(onListenEmitFrom: () async* {
63+
Stream<ConnectionStateUpdate> get connectedDeviceStream => Repeater.broadcast(onListenEmitFrom: () async* {
6464
await initialize();
6565
yield* _deviceConnector.deviceConnectionStateUpdateStream;
66-
}).stream.asBroadcastStream()
67-
..listen((_) {});
66+
}).stream;
6867

6968
/// A stream providing value updates for all the connected BLE devices.
7069
///
@@ -105,8 +104,7 @@ class FlutterReactiveBle {
105104
);
106105

107106
if (Platform.isAndroid || Platform.isIOS) {
108-
ReactiveBlePlatform.instance =
109-
const ReactiveBleMobilePlatformFactory().create(
107+
ReactiveBlePlatform.instance = const ReactiveBleMobilePlatformFactory().create(
110108
logger: _debugLogger,
111109
);
112110
}
@@ -398,11 +396,10 @@ class FlutterReactiveBle {
398396
Future<void> clearGattCache(String deviceId) =>
399397
_blePlatform.clearGattCache(deviceId).then((info) => info.dematerialize());
400398

401-
/// Reads the RSSI of the of the peripheral with the given device ID.
399+
/// Reads the RSSI of the of the peripheral with the given device ID.
402400
/// The peripheral must be connected, otherwise a [PlatformException] will be
403401
/// thrown
404-
Future<int> readRssi(String deviceId) async =>
405-
_blePlatform.readRssi(deviceId);
402+
Future<int> readRssi(String deviceId) async => _blePlatform.readRssi(deviceId);
406403

407404
/// Subscribes to updates from the characteristic specified.
408405
///

0 commit comments

Comments
 (0)