Is there a function to stop finding repeated beacons with altbeacon?

Loon :

I am trying to discover the distance between my device and the beacons that i have setup in my office for my intern project.I am still quite new to Android studios and java. The beacons are from apple(iBeacon).However i only managed to find out one of the beacons and the results is repeated even though i have five beacons turn on. I downloaded an app(Beacon Scanner) to ensure that the other beacons are turn on.I found out that the beacons are indeed turn on but i can only manage to find one beacon and the result is repeated.How do i stop the result from repeating and show the rest of the beacons?

Below is the code i got from the Altbeacon library developed by davidgyoung.

@Override
public void onBeaconServiceConnect() {

    RangeNotifier rangeNotifier = new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                Log.d(TAG, "didRangeBeaconsInRegion called with beacon count:  "+beacons.size());
                Beacon firstBeacon = beacons.iterator().next();
                logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + String.format("%.2f", firstBeacon.getDistance()) + " meters away.");
            }
        }
    };
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        beaconManager.addRangeNotifier(rangeNotifier);
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        beaconManager.addRangeNotifier(rangeNotifier);
    } catch (RemoteException e) {   }
}

private void logToDisplay(final String line) {
    runOnUiThread(new Runnable() {
        public void run() {
            EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText);
            editText.append(line+"\n");
        }
    });
}

This is the result from my device.https://i.stack.imgur.com/zJWs2.jpg

davidgyoung :

Understand that the callback didRangeBeaconsInRegion is called one per second with a list of all beacons detected since the last call. The code you show just looks at the first beacon:

Beacon firstBeacon = beacons.iterator().next();

If instead you want to process all of the beacons detected, use a loop:

   for (Beacon beacon : beacons) {
      logToDisplay("The beacon " + beacon.toString() + " is about " + String.format("%.2f", beacon.getDistance()) + " meters away.");
   }

If you don't want to redo the processing every second, then you might add a flag like this :

 boolean alreadyProcessed = false;
 @Override
  public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    if (!alreadyProcessed) {
      alreadyProcessed = true;
      for (Beacon beacon : beacons) {
        logToDisplay("The beacon " + beacon.toString() + " is about " + String.format("%.2f", beacon.getDistance()) + " meters away.");
      }
    }
  }

The above is a simple solution which tells you all beacons seen in one second. But keep in mind that all beacons might not be detected every second of they are not advertising frequently. You will need a more sophisticated solution to produce a list of all unique beacons seen over a longer period of time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154497&siteId=1