Search code examples
androidbeaconibeacon-androidestbeaconmanager

Trying to fetch a list of all my beacons nearby. but nothing is returned


I have all the permissions granted. I use the beaconManager. which I create like this:

 beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
    beaconManager.bind(this);

OnBeaconConnect I do this:

  @Override
public void onBeaconServiceConnect() {
    ArrayList<Identifier> identifiers = new ArrayList<>();

    // Set null to indicate that we want to match beacons with any value
    identifiers.add(null);
    // Represents a criteria of fields used to match beacon

    try {
        Region region = new Region("all-beacons-region", null, null, null);
        Region region2 = new Region("de3be3ce-4770-477b-8893-0f0e5aeb9b98", null, null, null);
        // Tells the BeaconService to start looking for beacons that match the passed Region object
        beaconManager.startRangingBeaconsInRegion(region);
        beaconManager.startRangingBeaconsInRegion(region2);
        beaconManager.startMonitoringBeaconsInRegion(region);
        beaconManager.startMonitoringBeaconsInRegion(region2);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    // Specifies a class that should be called each time the BeaconService gets ranging data, once per second by default
    beaconManager.addRangeNotifier(this);
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: " + state);
        }
    });

}

And didRangeBeaconsInRegion has this:

  @Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    if (beacons.size() > 0) {
        Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
    }
}

But the monitorNotifier never gets called, and the rangeNotifier gets called, but beacons size is always 0. Why don't I get the beacons here?

PS: I even tried hardcoding a beacon, using the UUID, but still I get nothing in monitorNotifier, and still size 0 in the range notifier


Solution

  • It looks to me like you set up three beacon parsers for the Eddystone formats, but you are trying to detect a beacon sending out the iBeacon format. (I say this based on the first identifier in the second region definition being a 16 byte UUID.) If this is indeed what you are trying to do, please add a beacon parser for the iBeacon format. You can find that here:

    https://beaconlayout.wordpress.com/

    Also, if you are trying to detect on Android 6+ make sure you have obtained location permissions otherwise you won't detect anything. See here:

    https://altbeacon.github.io/android-beacon-library/requesting_permission.html