alann-maulana / flutter_beacon

An hybrid iBeacon scanner and transmitter SDK for Flutter Android and iOS.
Apache License 2.0
120 stars 153 forks source link

how to stop beacon from ranging if already found #107

Open eggysudianto opened 3 years ago

eggysudianto commented 3 years ago

how to stop beacon from ranging if already found which beacon I want

this my code. _streamRanging.cancel(); is never called because beacon always ranging even though I break the looping

bool found = false;
var _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
        for (var element in result.beacons) {
          if (element.proximityUUID.toString() == proximityUUID
              && element.major.toString() == major
              && element.minor.toString() == minor){
            setState(() {
              found = true;  
            });

            print("SUCCESS");
            break;
          }
        }
      });

      if(found) {
        _streamRanging.cancel();
      }
KinectTheUnknown commented 3 years ago

You should store the stream subscription as a property in the widget so you can cancel it in the setState and set it to null

GeekSlts commented 3 years ago

If you don't need the loop, you can try removing the repeating values and only display your updated uniques-beacons.

Try to make a new Map of your ranging then join to the map the result with a forEach, you can delete all duplicates values with this:

setState(() {
_regionBeacons[result.region] = result.beacons;
          _beacons.clear();
          final Map<dynamic, Beacon> profileMap = new Map();
          result.beacons.forEach((item) {
            profileMap[item.macAddress] = item;
          });
          _beacons.addAll(profileMap.values);
          _beacons.sort(_compareParameters);
        });