Estimote / Android-Proximity-SDK

Estimote Proximity SDK for Android
https://developer.estimote.com
MIT License
82 stars 42 forks source link

How to find nearest beacon #73

Closed happyssm90 closed 6 years ago

happyssm90 commented 6 years ago

Prerequisites

Basic information

Estimote SDK version: [estimote:proximity-sdk:0.6.2]

Android devices affected: [Samsung, One Plus]

Android OS version affected: [Oreo 8.1]

Beacon hardware version: [G1.12]

Description

I am able to find all the beacons using Tag Name, But How to find nearest beacon among them ? Below is My code

`venueZone = proximityObserver.zoneBuilder()
                 .forTag("Loc1")
                 .inCustomRange(30)
                 .withOnEnterAction(new Function1<ProximityContext, Unit>() {
                     @Override
                     public Unit invoke(ProximityContext proximityContext) {
                         Log.e("SABYA", "withOnEnterAction");
                         return null;
                     }
                 })
                 .withOnExitAction(new Function1<ProximityContext, Unit>() {
                     @Override
                     public Unit invoke(ProximityContext proximityContext) {
                         Log.e("SABYA", "withOnExitAction");
                         return null;
                     }
                 })
                 .withOnChangeAction(new Function1<List<? extends ProximityContext>, Unit>() {
                     @Override
                     public Unit invoke(List<? extends ProximityContext> proximityContexts) {
                         Log.e("SABYA", "withOnExitAction");
                         Toast.makeText(MainActivity.this, "withOnExitAction", Toast.LENGTH_SHORT).show();

                // here I am getting all the Beacons using Tag name, But How to know which one is nearest ?
                         return null;
                     }
                })
                 .create();`
ldimitroff commented 6 years ago

In version 0.6.2 the onChangeAction returns a list that extends ProximityContext class. That list is sorted by proximity (nearest to farthest). So just getting the first one in the list would be enough.

ProximityContext nearest = proximityAttachments.get(0);

happyssm90 commented 6 years ago

Thanks @ldimitroff .. Is it same for proximity-sdk:1.0.0 ?

ldimitroff commented 6 years ago

I'd say it's the same thing, just get the first item in the set.

I don't know internally what kind of Set is implemented in the callback but to get the first item in the set is just simply set.iterator().next(); This is also really simple to test anyways.

pawelDylag commented 6 years ago

Hey @ldimitroff and @happyssm90

No, since version 1.0.0 there is a Set of nearby context returned. The set by definition is not sorted, so getting the first element won't give you a closest beacon. We intentionally made it impossible to distinguish between beacon ranges, because of the BLE interferences and error rate of radio waves. It's not very reliable to find the closest beacon, and we don't want to deliver something that is not reliable ;)

If you want to track individual beacon, please make a Zone for it, and write your desired distance using .withCustomRage(...).

Cheers, Paweł

ldimitroff commented 6 years ago

Tracking a individual beacon it's not the same as having a sorted list with the closest beacon first :)

I think @pawelDylag answer didn't provide a solution to @happyssm90 nor to me that I'm actually using the same approach (and thinking rolling back to 0.6.2)

This question should remain open and not with the invalid tag. Thanks

kamilwlf commented 6 years ago

onContextChange ProximityZoneContext> contexts return only one element, but it is called multiple times for different beacons. Is there option to return all beacons at once?