alann-maulana / flutter_beacon

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

Problem with Ranging Beacons after Initialization #42

Closed JamesSwag closed 4 years ago

JamesSwag commented 4 years ago

Hello, I seem to have a problem where I can't range new regions with new uuids. It ranges only for the initial uuid I set. I have tried canceling the stream then adding a new region to the list then subscribing again however it does not work and it only reacts to the initial uuid. I have my code below. As you can see the ranging method starts the stream. Then in the onPressed property of the second button I first Cancel the stream then remove the first uuid region and then add a new region before starting the stream again.

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_beacon/flutter_beacon.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String result;
  StreamSubscription<RangingResult> _streamRanging;
  final regions = <Region>[];

 ** ranging() async {
    try {
      await flutterBeacon.initializeAndCheckScanning;
    } catch (e) {}

    if (Platform.isIOS) {
      regions.add(Region(
          identifier: 'Apple Airlocate',
          proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
    } else {
      regions.add(Region(
          identifier: null,
          proximityUUID: '87b7696c-3700-4fb6-b274-d089dfc44686'));
    }

    _streamRanging =
        flutterBeacon.ranging(regions).listen((RangingResult result) {
      print(result.region);
    });
  }**

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: 100,
                child: MaterialButton(
                  child: Text("Subscribe"),
                  color: Colors.blue,
                 ** onPressed: () {
                    ranging();
                  },**
                ),
              ),
              Container(
                width: 100,
                child: MaterialButton(
                  child: Text("Cancel"),
                  color: Colors.blue,
               **   onPressed: () {
                    _streamRanging.cancel();
                    regions.removeLast();
                    regions.add(Region(
                        identifier: null,
                        proximityUUID: '98173b7d-2444-424a-bed9-7acf4bf97a50'));
                    _streamRanging = flutterBeacon
                        .ranging(regions)
                        .listen((RangingResult result) {
                      print(result.region);
                    });
                  },**
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
renoinn commented 4 years ago

did u try setState?

           **   onPressed: () {
                    _streamRanging.cancel();
                   setState(() {
                      regions.removeLast();
                      regions.add(Region(
                         identifier: null,
                         proximityUUID: '98173b7d-2444-424a-bed9-7acf4bf97a50'));
                      _streamRanging = flutterBeacon
                         .ranging(regions)
                         .listen((RangingResult result) {
                        print(result.region);
                      });
                   });
                  },**

or

        _streamRanging.cancel();
        final newRegions = <Region>[
          Region(
              identifier: null,
              proximityUUID: '98173b7d-2444-424a-bed9-7acf4bf97a50'),
        ];
       _streamRanging = flutterBeacon
                        .ranging(newRegions)
                        .listen((RangingResult result) {
          print(result.region);
        });
        setState(() {
          regions = newRegions;
        });
JamesSwag commented 4 years ago

Let me test it out then I will get back to you

JamesSwag commented 4 years ago

No unfortunately it does not work.

renoinn commented 4 years ago

@JamesSwag sorry, im wrong. flutterBeacon is caching FlutterEventChannel stream in flutterBeacon instance. https://github.com/alann-maulana/flutter_beacon/blob/master/lib/flutter_beacon.dart#L137

renoinn commented 4 years ago

I don't have any plan that fix this issue, cuz im not owner this plugin. One of user XD But, you can fork this repository and fix these files if you need it.

pubspec.yml

flutter_beacon:
  git:
    url: git@github.com:[your account]/flutter_beacon.git

flutter_beacon.dart

  Stream<RangingResult> ranging(List<Region> regions) {
-    if (_onRanging == null) {
      final list = regions.map((region) => region.toJson).toList();
      _onRanging = _rangingChannel
          .receiveBroadcastStream(list)
          .map((dynamic event) => RangingResult.from(event));
-    }
    return _onRanging;
  }
JamesSwag commented 4 years ago

I don't know anything about the inner workings of the plugin, so I wouldn't even know where to start. However does the plugin work for you? Are you able to stream for new uuids?

renoinn commented 4 years ago

This plugin can't restart different uuid. but you can fork and fix code. Try change it yourself.

JamesSwag commented 4 years ago

But I don't know how to.

alann-maulana commented 4 years ago

Hello,

Sorry a lot of busy lately on my office projects 😁

I have been solving this "caching FlutterEventChannel" for both ranging and monitoring some days ago. I hope I can commit and push it soon