0

I am new to Android Programming and the Altbeacon Library. I can range the beacon I want, but I need to handle not having the beacon in the area and display a dialog. I can display a dialog without issue, but I can't figure out how to catch a No Beacons Found in Area...help?!?!?

Here's my code (from the altbeacon example with slight modifications)...

 public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

                if (beacons.size() > 0) {

                    Beacon firstBeacon = beacons.iterator().next();
                        Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away."}
            }
        });


   try {
        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        // wake up the app when a beacon is seen
        String uuid = "MYCUSTOM UUID STRING";
        beaconManager.startRangingBeaconsInRegion(new Region(uuid, Identifier.parse(uuid), null, null));
    }
    catch (RemoteException e) {    }
}

I think an alternate solution would be to just do monitoring, but I need to get the Major and Minor values....can I do that from a monitoring activity?

davidbii
  • 105
  • 1
  • 1
  • 5

1 Answers1

1

You can combine monitoring with ranging so that you can still get beacon identifiers. Check following code (I altered your code).

public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {
                Beacon firstBeacon = beacons.iterator().next();
                Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
            }
        }
    });

    beaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            try {
                beaconManager.startRangingBeaconsInRegion(region);
            }
            catch (RemoteException e) {    }
        }

        @Override
        public void didExitRegion(Region region) {
            try {
                beaconManager.stopRangingBeaconsInRegion(region);
            }
            catch (RemoteException e) {    }
        }
    });


   try {
        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        // wake up the app when a beacon is seen
        String uuid = "MYCUSTOM UUID STRING";
        beaconManager.startMonitoringBeaconsInRegion(new Region(uuid, Identifier.parse(uuid), null, null));
    }
    catch (RemoteException e) {    }
}

So that with didEnterRegion, you can check whether a beacon has entered your region and with didExitRegion you can get whenever there is no beacon around (with region filter of course).

However, with this approach you can not get an event if there is no beacon around when monitoring started first. For this you can set a timer and set a flag in didEnterRegion. If that flag is not set in the time that you wanted, then you can be sure there is no beacon around. Other than first time, this approach should solve your problem.

Furkan Varol
  • 252
  • 2
  • 8
  • Thanks for the timer idea. For the flag are talking about setting a variable with the timer and than changing the variable when the beacon is found, and than an if statement at the end of the timer stating the beacon wasn't found? – davidbii Apr 07 '15 at 13:49
  • Yes, timer will act like a timeout. If you want, I can write an example. – Furkan Varol Apr 08 '15 at 05:29