|
1 | 1 | part of _internal;
|
2 | 2 |
|
3 | 3 | mixin ScanningMixin on FlutterBLE {
|
4 |
| - Stream<dynamic> _scanEvents; |
| 4 | + Stream<ScanResult> _scanEvents; |
5 | 5 |
|
6 | 6 | void _prepareScanEventsStream() {
|
7 |
| - _scanEvents = |
8 |
| - const EventChannel(ChannelName.scanningEvents).receiveBroadcastStream(); |
| 7 | + _scanEvents = const EventChannel(ChannelName.scanningEvents) |
| 8 | + .receiveBroadcastStream() |
| 9 | + .handleError( |
| 10 | + (errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)), |
| 11 | + test: (error) => error is PlatformException, |
| 12 | + ) |
| 13 | + .map( |
| 14 | + (scanResultJson) => |
| 15 | + ScanResult.fromJson(jsonDecode(scanResultJson), _manager), |
| 16 | + ); |
9 | 17 | }
|
10 | 18 |
|
11 | 19 | Stream<ScanResult> startDeviceScan(
|
12 | 20 | int scanMode,
|
13 | 21 | int callbackType,
|
14 | 22 | List<String> uuids,
|
15 | 23 | bool allowDuplicates,
|
16 |
| - ) async* { |
17 |
| - _methodChannel.invokeMethod( |
18 |
| - MethodName.startDeviceScan, |
19 |
| - <String, dynamic>{ |
20 |
| - ArgumentName.scanMode: scanMode, |
21 |
| - ArgumentName.callbackType: callbackType, |
22 |
| - ArgumentName.uuids: uuids, |
23 |
| - ArgumentName.allowDuplicates: allowDuplicates, |
24 |
| - }, |
25 |
| - ); |
26 |
| - |
| 24 | + ) { |
27 | 25 | if (_scanEvents == null) {
|
28 | 26 | _prepareScanEventsStream();
|
29 | 27 | }
|
30 | 28 |
|
31 |
| - yield* _scanEvents.handleError( |
32 |
| - (errorJson) { |
33 |
| - throw BleError.fromJson(jsonDecode(errorJson.details)); |
34 |
| - }, |
35 |
| - test: (error) => error is PlatformException, |
36 |
| - ).map((scanResultJson) => ScanResult.fromJson( |
37 |
| - jsonDecode(scanResultJson), |
38 |
| - _manager, |
39 |
| - )); |
| 29 | + StreamController<ScanResult> streamController = StreamController.broadcast( |
| 30 | + onListen: () => _methodChannel.invokeMethod( |
| 31 | + MethodName.startDeviceScan, |
| 32 | + <String, dynamic>{ |
| 33 | + ArgumentName.scanMode: scanMode, |
| 34 | + ArgumentName.callbackType: callbackType, |
| 35 | + ArgumentName.uuids: uuids, |
| 36 | + ArgumentName.allowDuplicates: allowDuplicates, |
| 37 | + }, |
| 38 | + ), |
| 39 | + onCancel: () => stopDeviceScan(), |
| 40 | + ); |
| 41 | + |
| 42 | + streamController |
| 43 | + .addStream(_scanEvents, cancelOnError: true) |
| 44 | + .then((_) => streamController?.close()); |
| 45 | + |
| 46 | + return streamController.stream; |
40 | 47 | }
|
41 | 48 |
|
42 | 49 | Future<void> stopDeviceScan() async {
|
|
0 commit comments