1
votes

I'm working with the AltBeacon library (2.5.1) to detect beacons. I setup ranging with an "universal" Region to be able to detect any beacon in range, then do my stuff with it. The issue is that when I have several beacons in range, the didRangeBeaconsInRegion callback always provides me a Collection of only 1 beacon at a time and this beacon is a random one among all the present beacons... Why can't I get all the beacons in range in my Collection ?

All of this is made from within a Service, I did clean all the other stuff to keep only the relevant parts of the code below -> Hopefully I am doing something wrong here ?

public class MonitorService extends Service implements BeaconConsumer 
{

    private BeaconManager beaconManager;


    @Override
    public void onCreate() 
    {
        super.onCreate();


        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

        beaconManager.setForegroundScanPeriod(5000l);
        beaconManager.setBackgroundScanPeriod(5000l);
        beaconManager.setForegroundBetweenScanPeriod(1100l);
        beaconManager.setBackgroundBetweenScanPeriod(1100l);


        setupBeaconManager();
    }


    private void setupBeaconManager()
    {
        if (!beaconManager.isBound(this))
            beaconManager.bind(this);
    }

    private void unsetBeaconManager()
    {
        if (beaconManager.isBound(this))
        {
            beaconManager.unbind(this);

            try 
            {
                beaconManager.stopRangingBeaconsInRegion(new Region("apr", null, null, null));
            } 
            catch (RemoteException e) 
            {  
                Log.i(TAG, "RemoteException = "+e.toString());
            }
        }
    }


    @Override
    public void onBeaconServiceConnect() 
    {
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override 
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) 
            {
                Log.i(TAG,"didRangeBeaconsInRegion, number of beacons detected = "+beacons.size());
                // HERE IT IS : the size is Always 1, but the beacon (UUID etc. can be different)
            }
        });


        try 
        {
            beaconManager.startRangingBeaconsInRegion(new Region("apr", null, null, null));
        } 
        catch (RemoteException e) 
        {  
            Log.i(TAG, "RemoteException = "+e.toString());
        }


    }

    @Override
    public void onDestroy() 
    {
        unsetBeaconManager();

        super.onDestroy();
    }


}

I'm working on Android 5.1.1 with a Nexus 6 (but a Wiko cheap phone gives the same results). The beacons are setup to advertise every 600ms... But even with 100ms it also gives the exact same results...

1

1 Answers

3
votes

The code looks OK. A couple of thoughts:

  • Try using an off the shelf beacon scanner app based on the same library like Locate. Does it detect all of your beacons simultaneously? If not, something may be wrong with the beacons or their configuration.

  • Do each of your beacons have unique identifiers? The library by default only detects multiple beacons if they have unique identifiers.