问题
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