How to know the closest iBeacon

点点圈 提交于 2019-11-30 22:56:22

First of all: Replace your long else/if of locationManager:didRangeBeacons:inRegion: with:

if (([closestBeacon.minor intValue] > 0 && [closestBeacon.minor intValue] <= 11) || [closestBeacon.minor intValue] == 30)
{
    actualSection = [closestBeacon.minor intValue]; 
    [self immediateDetection];
}

Replace your long else/if in immediateDetection with:

if ((actualSection > 0 && actualSection < 10 || actualSection == 30)
{
    SectionViewController *sectionView = [[SectionViewController alloc] init];
    sectionView.section = actualSection;
    [self presentViewController:sectionView animated:NO completion:nil];
    sectionView.lbl_name.text = self.lbl_name.text;
}
else if (actualSection == 10 || actualSection == 11)
{
    ThiefAlarmViewController *thiefAlarmView = [[ThiefAlarmViewController alloc] init];
    [self presentViewController:thiefAlarmView animated:NO completion:nil];
}
else
{
}

That should save you a lot of code.

For your issue, you could to this:

    NSPredicate *predicateIrrelevantBeacons = [NSPredicate predicateWithFormat:@"(self.accuracy != -1) AND ((self.proximity != %d) OR (self.proximity != %d))", CLProximityFar,CLProximityUnknown];
NSArray *relevantsBeacons = [beacons filteredArrayUsingPredicate: predicateIrrelevantBeacons];
    NSPredicate *predicateMin = [NSPredicate predicateWithFormat:@"self.accuracy == %@.@min.accuracy", relevantsBeacons];

    CLBeacon *closestBeacon = nil;
    NSArray *closestArray = [[relevantsBeacons filteredArrayUsingPredicate:predicateMin];
    if ([closestArray count] > 0)
        closestBeacon = [closestArray objectAtIndex:0];
   if (closestBeacon)
   { //Do your thing }
   else
   {//No relevant close beacon}

locationManager:didRangeBeacons:inRegion: will return you a list of beacons in proximity order. That is, the first beacon in the list will be the nearest to you.

However, there is one exception to this rule: if any beacons have a proximity of Unknown, then they will be at the top of the list (because their accuracy is -1). To account for this, simply filter out any Unknown beacons, and then pick the first in the list:

beacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity != %d", CLProximityUnknown]];
CLBeacon *nearestBeacon = [beacons firstObject];

In addition to filtering out proximity values of -1, you also have to take into account cases where certain iBeacons periodically do not appear in a callback to didRangeBeacons: inRegion. This can happen due to radio interference, especially for battery powered beacons that do not transmit advertisements very frequently.

To solve this, you need to keep your own list of recently seen beacons, and track any beacons that have been seen in the last few seconds. If one of the beacons has a proximity of -1, you can either not put it in the list, or re-use the last known non -1 proximity value if it does not exist.

Another consideration that you need to take into account is that the proximity value will bounce around quite a bit. You may need to add some hysteresis logic to your algorithm so which beacon is the closest doesn't bounce back and forth every second. The specifics of how you want to do this depend on your use case.

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