@@ -25,7 +25,7 @@ mixin DeviceConnectionMixin on FlutterBLE {
2525
2626 Stream <PeripheralConnectionState > observePeripheralConnectionState (
2727 String identifier, bool emitCurrentValue) {
28- var controller = StreamController <String >(
28+ var controller = StreamController <PeripheralConnectionState >(
2929 onListen: () => _methodChannel.invokeMethod (
3030 MethodName .observeConnectionState,
3131 < String , dynamic > {
@@ -37,18 +37,15 @@ mixin DeviceConnectionMixin on FlutterBLE {
3737 ),
3838 );
3939
40- controller
41- .addStream (_peripheralConnectionStateChanges)
42- .then ((value) => controller? .close ());
43-
44- return controller.stream
45- .map ((jsonString) =>
46- ConnectionStateContainer .fromJson (jsonDecode (jsonString)))
47- .where ((connectionStateContainer) =>
48- connectionStateContainer.peripheralIdentifier == identifier)
49- .map ((connectionStateContainer) =>
50- connectionStateContainer.connectionState)
51- .map ((connectionStateString) {
40+ var sourceStream =
41+ _peripheralConnectionStateChanges
42+ .map ((jsonString) =>
43+ ConnectionStateContainer .fromJson (jsonDecode (jsonString)))
44+ .where ((connectionStateContainer) =>
45+ connectionStateContainer.peripheralIdentifier == identifier)
46+ .map ((connectionStateContainer) =>
47+ connectionStateContainer.connectionState)
48+ .map ((connectionStateString) {
5249 switch (connectionStateString.toLowerCase ()) {
5350 case NativeConnectionState .connected:
5451 return PeripheralConnectionState .connected;
@@ -64,6 +61,15 @@ mixin DeviceConnectionMixin on FlutterBLE {
6461 );
6562 }
6663 });
64+
65+ controller
66+ .addStream (
67+ sourceStream,
68+ cancelOnError: true ,
69+ )
70+ .then ((value) => controller? .close ());
71+
72+ return controller.stream;
6773 }
6874
6975 Future <bool > isPeripheralConnected (String peripheralIdentifier) async {
0 commit comments