How to detect multiple beacons using Altbeacon android library?
Asked Answered
P

1

7

I am using the AltBeacon sample app on my android device - the sample app provided by altbeacon.org is here: https://github.com/AltBeacon/android-beacon-library-reference

However, the app when launched is detecting and displaying only one beacon. I have about 5 beacons near my Android device. How do I detect all the beacons?

In RangingActivity.java, I noticed this method that is being called when a beacon comes in sight:

public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            EditText editText = (EditText) RangingActivity.this.findViewById(R.id.rangingText);                    
                    Beacon firstBeacon = beacons.iterator().next();
                    logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
            }
        }
    }

I modified the iterator to read from the collection in a while loop as follows:

     Beacon firstBeacon;
     while(beacons.iterator().hasNext()){
                firstBeacon = beacons.iterator().next();
                logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
            }

However, the app crashes with this modification.

My questions:

(1) How can I display all beacons that are near my Android device?

(2) How can I detect beacons that go out of region?

Propeller answered 24/1, 2015 at 18:48 Comment(1)
Could you add the logcat output when it crashed to your question?Neruda
S
5

For 1. I think you need to use a For loop. Like this.

for (Beacon beacon : beacons) {
    logToDisplay("The beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away.");
}

For 2. I was having trouble detecting that, but it might be a long timeout. So be very patient. I think the code in the Monitoring activity can be altered to display a message for that. Or you can review the logcat from a device. A simple logToDisplay can be used inside the didExitRegion section of BeaconReferenceApplication.

public void didExitRegion(Region region) {
    if (monitoringActivity != null) {
        monitoringActivity.logToDisplay("I no longer see a beacon in the "+region.getUniqueId());
    }
}
Subshrub answered 24/1, 2015 at 19:12 Comment(1)
Using a loop is the right answer. The reference app just shows the distance to the first beacon seen for simplicityCortege

© 2022 - 2024 — McMap. All rights reserved.