Multiple listeners on one BeaconManager

Hi,

I’m actually trying to work with multiple region ranging. When I call the setRangingListener(), does it add a new listener or replace the previous (if it exists) listener?

In other words, can I do this:

BeaconRangingListener listener1 = new BeaconRangingListener {
    @Override
    public void onBeaconsDiscovered(BeaconRegion beaconRegion, List<Beacon> list) {
        Log.d(TAG, "Called callback for region 1.");
    }
});
BeaconRangingListener listener2 = new BeaconRangingListener {
    @Override
    public void onBeaconsDiscovered(BeaconRegion beaconRegion, List<Beacon> list) {
        Log.d(TAG, "Called callback for region 2.");
    }
});

beaconManager.setRangingListener(listener1);
beaconManager.startRanging(beaconRegion1);

beaconManager.setRangingListener(listener2);
beaconManager.startRanging(beaconRegion2);

Does it works?

Nope.

If you use setRangingListener(), the older listener will be destroyed.
In this case listener1 will be discarded and only listener2 will discover beacons.

Ty for replying.

Did you test it? Because it seems to me that it works…

Hmmm, I only provide the information from Android SDK Javadocs and I never tried to use more then one Listener.

For example:
I use only one EddystoneListener, but then I just filter the data and use it for different purposes.
onEddystonesfound deliver a list: List< Eddystone > list.
Later I use this list as parameter for functions like:

method1(list)
method2(list)

All this methods use different data of beacons. For example method2 looks, whether there are some Eddystone-TLM data or not. (Only for example)

All in one: I dont need more then one listener and I think, that multiple listener eat the battery and other ressources of a device and create unnecessary overhead.

In your case you must define Region for all beacons:

    public BeaconRegion(java.lang.String identifier,
                        java.util.UUID proximityUUID,
                        java.lang.Integer major,
                        java.lang.Integer minor)
    Parameters:identifier - A unique identifier for a region.Cannot be null.proximity
UUID - Location UUID of beacons.Can be null. Null indicates all location UUIDs.
major - Major version of the beacons. Can be null. Null indicates all major versions.
minor - Minor version of the beacons. Can be null. Null indicates all minor versions.

OK, but I want to range 2 regions (not to have to filter above), so my listener would be like this:

this.myBeaconManager.setRangingistener(new BeaconManager.BeaconRangingListener {
    @Override
    public void onBeaconsDiscovered(BeaconRegion beaconRegion, List<Beacon> list) {
        if (beaconRegion.equals(this.myBeaconRegion1)) {
            // Handle region 1 discovering callback.
        } else {
            if (beaconRegion.equals(this.myBeaconRegion2)) {
                // Handle region 2 discovering callback.
            }
        }
    }
});

do it in this way without using more then one listener!

Yes, but I will after add dynamically multiple regions to range, so I will have to map a region with a callback.
I was questioning because I’m actually working with a plug-in in which listeners are added like this.

try to create a HashMap < String, BeaconRegion > and just iterate through it or use HashMap.containsKey() function.
If there is a new region, just put it in a hashmap with put(“bla bla bla”,new BeaconReagion(…))

I will try it tomorrow!
Ty @alexander :slight_smile:

you’re welcome :robot: