alann-maulana / flutter_beacon

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

Multiple Beacon Detection #9

Closed JPyke3 closed 5 years ago

JPyke3 commented 5 years ago

HI!

Firstly thanks for the great Plugin! I have had some pretty good success with it and its a really good solution!

Just one problem that I have had, I am trying to detect multiple beacons at once, I thought this would be fairly easy because in order to range the beacons you have to accept a list, however for some reason the region only accepts the first list item.

I have figured out that when i listen to the stream like in the line beaconStream.listen(print); it does output all the beacons, However then it outputs two beacons and a duplicate beacon.

The reason I would like to do this, is i would like to output the beacons to a streambuilder widget and build a list of the beacons.

Here is my code:

  Future<List<Region>> fetchBeacons() async {
    List<Region> regions = [];
    regions = [
      Region(
          proximityUUID: 'AFC0FF69-3ACB-4A99-9F6A-2A4B6F786619',
          identifier: 'T-Shirt Beacon',
          major: 1,
          minor: 1),
      Region(
          proximityUUID: '705DAEE5-56BC-415A-839B-4AE00FC29946',
          identifier: 'Test',
          major: 1,
          minor: 1),
    ];
    return regions;
  }

  Future<Stream<RangingResult>> scanBeacons() async {
    await flutterBeacon.initializeScanning;
    List<Region> regions = await fetchBeacons();
    Stream<RangingResult> beaconStream = flutterBeacon.ranging(regions);
    beaconStream.listen(print);
    return beaconStream;
  }Ï

And this is the stream response to the console from the streambuilder:

RangingResult{"region": {"identifier":"T-Shirt Beacon","proximityUUID":"AFC0FF69-3ACB-4A99-9F6A-2A4B6F786619","major":1,"minor":1}, "beacons": []}

And from the beaconStream.listen

flutter: RangingResult{"region": {"identifier":"Test","proximityUUID":"705DAEE5-56BC-415A-839B-4AE00FC29946","major":1,"minor":1}, "beacons": []}
flutter: RangingResult{"region": {"identifier":"T-Shirt Beacon","proximityUUID":"AFC0FF69-3ACB-4A99-9F6A-2A4B6F786619","major":1,"minor":1}, "beacons": []}
flutter: RangingResult{"region": {"identifier":"T-Shirt Beacon","proximityUUID":"AFC0FF69-3ACB-4A99-9F6A-2A4B6F786619","major":1,"minor":1}, "beacons": []}
JPyke3 commented 5 years ago

Apologies, My error was related to how i was dealing with the stream further down the line. Again, thanks again for the plugin! Its super good! I really appreciate it!