Kontakt beacon how to retrieve beacon based on accuracy

白昼怎懂夜的黑 提交于 2019-12-25 03:44:36

问题


I need to scan when the beacons are near but what I need is that the beacon that gets find is the nearest not a random one. I tried with both methods:

public void onBeaconsUpdated(final Region region, final List<BeaconDevice> beacons) {
            for(int i=0; i<beacons.size();i++)
            {
                beacon = beacons.get(i);
                if(beacon.getProximity() != Proximity.UNKNOWN && beacon.getAccuracy() < 4)
                {
                    i = beacons.size() + 1;
                }
            }//THIS IS THE METHOD I USE TO GET THE NEAREST BEACON FROM THE LIST BUT IT DOESN'T WORK

                if(beacon.getMinor() == 1 && beacon.getMinor() != lastBeacon)
                {
                }
                if(beacon.getMinor() == 2 && beacon.getMinor() != lastBeacon)
                {
                }
                if(beacon.getMinor() == 3 && beacon.getMinor() != lastBeacon)
                {
                }
                if(beacon.getMinor() == 4 && beacon.getMinor() != lastBeacon)
                {
                }
                if(beacon.getMinor() == 5 && beacon.getMinor() != lastBeacon)
                {
                }
//and so on
    }

@Override
public void onBeaconAppeared(final Region region, final BeaconDevice beacon) {
}

For the first method the list is not ordered based on the accuracy, On the second method the beacon gets found not based on their accuracy. Is it possible for the first method to order the list in order to have the NEAREST beacon?

EDIT: I also have another problem, when I find a beacon i'd like to stop monitoring for other but calling beaconManager.stopMonitoring(); doesn't work and the app keeps searching for beacons.. is it the correct method?


回答1:


During the BeaconManager's configuration part, set BeaconDevice.DistanceSort, with ASC value please.

 BeaconManager.setDistanceSort(DistanceSort.ASC);

You can then easily retrieve Beacon instance from the list by specifying:

  ublic void onBeaconsUpdated(final Region region, final List<BeaconDevice> beacons) {
        final BeaconDevice beaconDevice = beacons.get(0); // the nearest beacon device.
}

I see as well that you're interested in lastBeacon minor value and several major values. You can as well apply filter that will handle the acceptance only of the Beacons with the lastBeacon minor value and several major values. You can do it with MinorFilter, MajorFilter and CustomFilter.

If 0 beacons are nearby the method public void onMonitorStop() gets called, is there any way to prevent this method to get called and keep scanning?

Both onMonitorStart() and onMonitorStop() are tightly bound to the MonitorPeriod. The MonitorPeriod.getActivePeriod() specifies how long monitoring lasts and MonitorPeriod.getPassivePeriod() tells how long the Android device is inactive between 2 following active periods.

I see that you're interested in onBeaconsUpdated() method mostly so you could alternatively register RangingListener as it provides single method where you get Region and list of the devices.

Ranging employs a bit less resources than monitoring and is preferable if you are focused on the remote devices rather than regions entering/abandoning.



来源:https://stackoverflow.com/questions/30503258/kontakt-beacon-how-to-retrieve-beacon-based-on-accuracy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!