ios - didRangeBeacons 委托(delegate)调用 CLProximityNear 和 CLProximityFar 太快了

标签 ios objective-c timer location ibeacon

我想插入一个计时器以避免在“远”、“近”、“立即”状态之间切换回来。

我对“远”和“近”状态使用相同的 View ,但我为即时状态推送一个新 View 。

因此对于直接返回 root 的情况,我已经通过这样做找到了解决方案:

[self performSelector:@selector(patchSelectorPopToRoot) withObject:nil afterDelay:4];

如果我使用相同的 View ,我该如何处理“近”和“远”状态?

这是代表:

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    // Descriptor on distance to sort the array of beacons by distance
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    // Sorting the array of beacons
    // Beacon array is sorted based on distance
    // Closest beacon is the first one
    self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

    if([self.beaconsArray count] > 0)
    {

        if(!self.selectedBeacon)
        {
            // initialy pick closest beacon
            self.selectedBeacon = [beacons objectAtIndex:0];
            currentBeaconMinor = self.selectedBeacon.minor;
        }
        else
        {

            //Sorting the array of beacons
           self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

           //Updating the selected beacon with the first element of the array (closest beacon)
           if(self.selectedBeacon != [beacons objectAtIndex:0] )
            {
                self.selectedBeacon = [beacons objectAtIndex:0];
                currentBeaconMinor = self.selectedBeacon.minor;
            }

        }

        // Switch on proximity of the closest beacon
        switch (self.selectedBeacon.proximity)
        {
            case CLProximityUnknown:
            {
                [self DoOnProximityUnknow];

                break;
            }
            case CLProximityImmediate:
            {
                [self DoOnProximityImmediate];

                break;
            }
            case CLProximityNear:
            {
                [self DoOnProximityNear];

                break;

            }
            case CLProximityFar:
            {
                [self DoOnProximityFar];

                break;
            }

            default:
                break;

        }
        self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];
    }
}

这是我想使用定时器(Near)的方法。

-(void)DoOnProximityNear
{
    //Starting a timer

    //not working :
    //[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timerCalled) userInfo:nil repeats:NO];


    //not working
    /*
    double delayInSeconds = 20.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Do some work");
    });*/
}

我只想在“Near”方法中设置一个 20 秒的计时器。我想在回到“Far”之前至少停留 20 秒。

我应该在哪里插入这个计时器?我应该等待远距离信号,启动计时器并等待另一个远距离信号,还是应该在到达近距离时启动计时器?

请问我该如何解决这个问题?

预先感谢您的帮助。

最佳答案

在我过去的一项工作中,需要同样的东西。我希望以下内容之一对您有所帮助。在我的例子中,我的逻辑是基于 RSSI(它也经常波动)

方式一 为 CLProximityFar 和 CLProximityNear 放置一个计数器,并根据该触发 Action ,计算相同 Proximity 的连续出现次数,如果是,则考虑该 PROximity 并触发其 Action 。

方法 2 这完全基于 RSSI 值。为此,您需要使用 10-20 个连续的 RSSI 值,取它们的平均值并在此基础上触发操作。

关于ios - didRangeBeacons 委托(delegate)调用 CLProximityNear 和 CLProximityFar 太快了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28335150/

相关文章:

ios - CLLocationManager startMonitoringForRegion 会使用 desiredAccuracy 吗?

ios - 如何在swift中停止功能

ios - 将字符串转换为核心位置

ios - 如何在 iOS 中创建一个非常自定义的 uibutton,如 UITableViewCellStyleSubtitle

ios - 如何以编程方式让 UIWebView 暂停/播放视频内容

c# - 如何防止 C# System.Timers 计时器事件阻塞后续事件?

arrays - 循环中的多个计时器

iOS Todays 小部件扩展 - 动画 UISlider

objective-c - 不使用 XCode 进行 Iphone 开发

iphone - 如何在 iPhone 的 Objective-C 中将 Google Directions API 折线字段解码为纬度长点?