0
votes

I am trying to range beacons and I am doing exactly what it is written here: http://altbeacon.github.io/android-beacon-library/samples.html

didRangeBeaconsInRegion method is firing but always with empty collection.

I have installed "locate" by altbeacon and it located 3 beacons.

this is my code: am I missing something?

public  class BeaconSingletone implements BeaconConsumer {

    private static BeaconSingletone instance;

    private final org.altbeacon.beacon.BeaconManager beaconManager2;
    private  ArrayList<BeaconThin> listNearBeacons = new ArrayList<>();




    BeaconRegion region = new BeaconRegion("ranged region",
                              UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null);

    private List<EventInterface> listeners = new ArrayList<EventInterface>();

    private BeaconSingletone()
    {

        beaconManager2 = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(ar_activity.get());
        // To detect proprietary beacons, you must add a line like below corresponding to your beacon
        // type.  Do a web search for "setBeaconLayout" to get the proper expression.
         beaconManager2.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager2.bind(this);



    public static BeaconSingletone getInstance() {
        if(instance == null) {
            instance = new BeaconSingletone();
        }
        return instance;
    }
    private void showNotification(Region region, final List<Beacon> list) {
        listNearBeacons.clear();
        listNearBeacons.add(new BeaconThin(3514,7580,-1));
        for (Iterator<EventInterface> i = listeners.iterator(); i.hasNext(); ) {
            EventInterface item = i.next();
            //item.NewBeaconFound(list.get(0).getMajor(),list.get(0).getMinor(),Utils.computeAccuracy(list.get(0)));
            item.NewBeaconsFound(listNearBeacons);
        }
        return;




    }



    @Override
    public void onBeaconServiceConnect() {
        beaconManager2.addRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<org.altbeacon.beacon.Beacon> beacons, org.altbeacon.beacon.Region region) {
                if (beacons.size() > 0) {
                    Log.i("BeaconManager", "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
                }
            }
        });
        try {
            beaconManager2.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("mybeacons", null, null, null));
        } catch (RemoteException e) {    }{


    }

    }

    @Override
    public Context getApplicationContext() {
        return ar_activity.get();
    }

    @Override
    public void unbindService(ServiceConnection serviceConnection) {
        ar_activity.get().unbindService(serviceConnection);
    }

    @Override
    public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
        return ar_activity.get().bindService(intent, serviceConnection, i);
    }
}
1

1 Answers

1
votes

You need to add a beacon layout for the beacon type you are using (iBeacon?). Replace this line:

beaconManager2.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

With the proper layout from here: https://beaconlayout.wordpress.com