@@ -7,20 +7,41 @@ mixin DeviceConnectionMixin on FlutterBLE {
7
7
8
8
Future <void > connectToPeripheral (String deviceIdentifier, bool isAutoConnect,
9
9
int requestMtu, bool refreshGatt, Duration timeout) async {
10
- return await _methodChannel
11
- .invokeMethod (MethodName .connectToDevice, < String , dynamic > {
12
- ArgumentName .deviceIdentifier: deviceIdentifier,
13
- ArgumentName .isAutoConnect: isAutoConnect,
14
- ArgumentName .requestMtu: requestMtu,
15
- ArgumentName .refreshGatt: refreshGatt,
16
- ArgumentName .timeoutMillis: timeout? .inMilliseconds
17
- }).catchError ((errorJson) =>
18
- Future .error (BleError .fromJson (jsonDecode (errorJson.details))));
10
+ return await _methodChannel.invokeMethod (
11
+ MethodName .connectToDevice,
12
+ < String , dynamic > {
13
+ ArgumentName .deviceIdentifier: deviceIdentifier,
14
+ ArgumentName .isAutoConnect: isAutoConnect,
15
+ ArgumentName .requestMtu: requestMtu,
16
+ ArgumentName .refreshGatt: refreshGatt,
17
+ ArgumentName .timeoutMillis: timeout? .inMilliseconds
18
+ },
19
+ ).catchError (
20
+ (errorJson) => Future .error (
21
+ BleError .fromJson (jsonDecode (errorJson.details)),
22
+ ),
23
+ );
19
24
}
20
25
21
26
Stream <PeripheralConnectionState > observePeripheralConnectionState (
22
- String identifier, bool emitCurrentValue) async * {
23
- yield * _peripheralConnectionStateChanges
27
+ String identifier, bool emitCurrentValue) {
28
+ var controller = StreamController <String >(
29
+ onListen: () => _methodChannel.invokeMethod (
30
+ MethodName .observeConnectionState,
31
+ < String , dynamic > {
32
+ ArgumentName .deviceIdentifier: identifier,
33
+ ArgumentName .emitCurrentValue: emitCurrentValue,
34
+ },
35
+ ).catchError (
36
+ (errorJson) => throw BleError .fromJson (jsonDecode (errorJson.details)),
37
+ ),
38
+ );
39
+
40
+ controller
41
+ .addStream (_peripheralConnectionStateChanges)
42
+ .then ((value) => controller? .close ());
43
+
44
+ return controller.stream
24
45
.map ((jsonString) =>
25
46
ConnectionStateContainer .fromJson (jsonDecode (jsonString)))
26
47
.where ((connectionStateContainer) =>
@@ -39,32 +60,32 @@ mixin DeviceConnectionMixin on FlutterBLE {
39
60
return PeripheralConnectionState .disconnecting;
40
61
default :
41
62
throw FormatException (
42
- "Unrecognized value of device connection state. Value: $connectionStateString " );
63
+ 'Unrecognized value of device connection state. Value: $connectionStateString ' ,
64
+ );
43
65
}
44
66
});
45
-
46
- _methodChannel.invokeMethod (
47
- MethodName .observeConnectionState, < String , dynamic > {
48
- ArgumentName .deviceIdentifier: identifier,
49
- ArgumentName .emitCurrentValue: emitCurrentValue,
50
- }).catchError (
51
- (errorJson) => throw BleError .fromJson (jsonDecode (errorJson.details)));
52
67
}
53
68
54
69
Future <bool > isPeripheralConnected (String peripheralIdentifier) async {
55
70
return await _methodChannel
56
71
.invokeMethod (MethodName .isDeviceConnected, < String , dynamic > {
57
72
ArgumentName .deviceIdentifier: peripheralIdentifier,
58
- }).catchError ((errorJson) =>
59
- Future .error (BleError .fromJson (jsonDecode (errorJson.details))));
73
+ }).catchError (
74
+ (errorJson) => Future .error (
75
+ BleError .fromJson (jsonDecode (errorJson.details)),
76
+ ),
77
+ );
60
78
}
61
79
62
80
Future <void > disconnectOrCancelPeripheralConnection (
63
81
String peripheralIdentifier) async {
64
82
return await _methodChannel
65
83
.invokeMethod (MethodName .cancelConnection, < String , dynamic > {
66
84
ArgumentName .deviceIdentifier: peripheralIdentifier,
67
- }).catchError ((errorJson) =>
68
- Future .error (BleError .fromJson (jsonDecode (errorJson.details))));
85
+ }).catchError (
86
+ (errorJson) => Future .error (
87
+ BleError .fromJson (jsonDecode (errorJson.details)),
88
+ ),
89
+ );
69
90
}
70
91
}
0 commit comments