Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect multiple beacons using Altbeacon android library?

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?

like image 888
Ninja Avatar asked Jan 24 '15 18:01

Ninja


1 Answers

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());
    }
}
like image 77
usernotdev Avatar answered Oct 19 '22 20:10

usernotdev