How to get the Major value when ranging with UUID in Android App

Hello,

I am trying to make an android app that will only allow you to login if you are in range of a beacon. I will have all of my beacons have the same UUID and will then identify which beacon it is with the major value.

So far I’ve got it to give a notification when a beacon with my unique UUID is in range but can’t figure out how to get the beacons Major value?

I followed step 2 of the Android tutorial (background monitoring) and created a java class called “BeaconChecker.java” instead of “MyApplication.java” like in the tutorial.

I know I need to use getMajor() but as I’m new to Java and Android I have no idea how to do this…

If I could get the Major before the showNotification() function and somehow pass it to LoginActivity.java I will be able to complete my application. Please see the code for monitoring for beacon and pushing notification in BeaconChecker.java class:

 beaconManager = new BeaconManager(getApplicationContext());

        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                beaconManager.startMonitoring(new Region(
                        "monitored region",
                        UUID.fromString("MY UNIQUE UUID"),
                        null, null));
            }
        });

        beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
            @Override
            public void onEnteredRegion(Region region, List<Beacon> list) {
##### Somehow get the beacon major id here and pass to LoginActivity.java #####
                showNotification(
                        "Beacon Detected!",
                        "Login enabled.");
            }
            @Override
            public void onExitedRegion(Region region) {
                // could add an "exit" notification too if you want (-:
            }
        });

Any help would be very much appreciated! Thanks!

The classical approach here is: in your onEnteredRegion, start ranging for the region, and then you’ll start receiving calls to your RangingListener's onBeaconsDiscovered, and in there you’ll have access to majors and minors of the beacons in range, including the one that triggered the enter.

(And it’s a good practice not to forget to stop ranging in onExited, since ranging is more energy intensive than monitoring.)

1 Like

Thank you for information. My issue is similar to this only. Let say we are in range of multiple beacon. I’m monitoring using only uuid. Now if i enter a region i want to keep tracking which is nearest beacon that is their major and minor. So as suggested to use Ranging in background, will it be a good practise to range continuously in background. My user may remain in region for hours.