@@ -13,18 +13,16 @@ class ConnectivityProbe extends StreamProbe {
1313 @override
1414 Future <bool > onStart () async {
1515 // collect the current connectivity status on sampling start
16- var connectivityStatus =
17- await connectivity.Connectivity ().checkConnectivity ();
18- addMeasurement (Measurement .fromData (
19- Connectivity .fromConnectivityResult (connectivityStatus)));
16+ var connectivityStatus = await connectivity.Connectivity ().checkConnectivity ();
17+ addMeasurement (Measurement .fromData (Connectivity .fromConnectivityResult (connectivityStatus)));
2018
2119 return super .onStart ();
2220 }
2321
2422 @override
25- Stream <Measurement > get stream =>
26- connectivity. Connectivity (). onConnectivityChanged. map ((event) =>
27- Measurement .fromData (Connectivity .fromConnectivityResult (event)));
23+ Stream <Measurement > get stream => connectivity. Connectivity ()
24+ . onConnectivityChanged
25+ . map ((event) => Measurement .fromData (Connectivity .fromConnectivityResult (event)));
2826}
2927
3028// This probe requests access to location permissions (both on Android and iOS).
@@ -69,24 +67,17 @@ class BluetoothProbe extends BufferingPeriodicStreamProbe {
6967 Stream <dynamic > get bufferingStream => FlutterBluePlus .scanResults;
7068
7169 @override
72- Future <Measurement ?> getMeasurement () async =>
73- _data != null ? Measurement .fromData (_data! ) : null ;
70+ Future <Measurement ?> getMeasurement () async => _data != null ? Measurement .fromData (_data! ) : null ;
7471
7572 // if a BT-specific sampling configuration is used, we need to
7673 // extract the services and remoteIds from it so FlutterBluePlus can
7774 // perform filtered scanning
78- List <Guid > get services => (samplingConfiguration
79- is BluetoothScanPeriodicSamplingConfiguration )
80- ? (samplingConfiguration as BluetoothScanPeriodicSamplingConfiguration )
81- .withServices
82- .map ((e) => Guid (e))
83- .toList ()
75+ List <Guid > get services => (samplingConfiguration is BluetoothScanPeriodicSamplingConfiguration )
76+ ? (samplingConfiguration as BluetoothScanPeriodicSamplingConfiguration ).withServices.map ((e) => Guid (e)).toList ()
8477 : [];
8578
86- List <String > get remoteIds => (samplingConfiguration
87- is BluetoothScanPeriodicSamplingConfiguration )
88- ? (samplingConfiguration as BluetoothScanPeriodicSamplingConfiguration )
89- .withRemoteIds
79+ List <String > get remoteIds => (samplingConfiguration is BluetoothScanPeriodicSamplingConfiguration )
80+ ? (samplingConfiguration as BluetoothScanPeriodicSamplingConfiguration ).withRemoteIds
9081 : [];
9182
9283 @override
@@ -97,8 +88,7 @@ class BluetoothProbe extends BufferingPeriodicStreamProbe {
9788 FlutterBluePlus .startScan (
9889 withServices: services,
9990 withRemoteIds: remoteIds,
100- timeout: samplingConfiguration? .duration ??
101- const Duration (milliseconds: DEFAULT_TIMEOUT ),
91+ timeout: samplingConfiguration? .duration ?? const Duration (milliseconds: DEFAULT_TIMEOUT ),
10292 );
10393 } catch (error) {
10494 FlutterBluePlus .stopScan ();
@@ -132,19 +122,15 @@ class BeaconProbe extends StreamProbe {
132122 super .samplingConfiguration as BeaconRangingPeriodicSamplingConfiguration ;
133123
134124 List <Region > get beaconRegions =>
135- samplingConfiguration? .beaconRegions
136- .map ((region) => region.toRegion ())
137- .toList () ??
138- [];
125+ samplingConfiguration? .beaconRegions.map ((region) => region.toRegion ()).toList () ?? [];
139126
140127 int get beaconDistance => samplingConfiguration? .beaconDistance ?? 2 ;
141128
142129 @override
143130 bool onInitialize () {
144131 super .onInitialize ();
145132 if (beaconRegions.isEmpty) {
146- warning (
147- '$runtimeType - No beacon regions specified for monitoring. Will not start monitoring.' );
133+ warning ('$runtimeType - No beacon regions specified for monitoring. Will not start monitoring.' );
148134 return false ;
149135 }
150136
@@ -165,15 +151,17 @@ class BeaconProbe extends StreamProbe {
165151 }
166152
167153 @override
168- Stream <Measurement > get stream => flutterBeacon.ranging (beaconRegions).map (
169- (RangingResult result) {
170- final closeBeacons = result.beacons
171- .where ((beacon) => beacon.accuracy <= beaconDistance);
172-
173- // If no beacons are close, still return an measurement with an empty
174- // list of iBeacons for this region.
175- return Measurement .fromData (
176- BeaconData (region: result.region.identifier)
154+ Stream <Measurement > get stream async * {
155+ await for (final monitoringResult in flutterBeacon.monitoring (beaconRegions)) {
156+ if (monitoringResult.monitoringState == MonitoringState .inside) {
157+ info ('$runtimeType - Entered region: ${monitoringResult .region .identifier }' );
158+
159+ yield * flutterBeacon.ranging (beaconRegions).map (
160+ (rangingResult) {
161+ final closeBeacons = rangingResult.beacons.where ((beacon) => beacon.accuracy <= beaconDistance);
162+
163+ return Measurement .fromData (
164+ BeaconData (region: rangingResult.region.identifier)
177165 ..scanResult = closeBeacons
178166 .map ((beacon) => BeaconDevice (
179167 uuid: beacon.proximityUUID,
@@ -183,7 +171,13 @@ class BeaconProbe extends StreamProbe {
183171 accuracy: beacon.accuracy,
184172 proximity: beacon.proximity,
185173 ))
186- .toList ());
187- },
188- );
174+ .toList (),
175+ );
176+ },
177+ );
178+ } else if (monitoringResult.monitoringState == MonitoringState .outside) {
179+ info ('$runtimeType - Exited region: ${monitoringResult .region .identifier }' );
180+ }
181+ }
182+ }
189183}
0 commit comments