ios - iOS iBeacon 如何设置扫描间隔时间?

标签 ios objective-c core-location ibeacon estimote

我正在开发一个支持 iBeacon 的应用程序。基本上,我有一个单一 View 根据最近的信标更新其内容,并且为了扫描信标,我没有使用任何框架(只是来自 Apple 的 CoreLocation),即使我使用 Estimote Beacons。 p>

问题是我的应用程序没有立即检测到信标,我必须在信标前等待大约 5 秒才能更新内容,而 Estimote 应用程序会在 1-2 秒内检测到信标。我的 Estimote 信标配置为每 960 毫秒发布一次广告。

这怎么可能?我可以设置一个时间间隔来扫描信标吗?如何改进我的应用以更快地更新 View ?

这是我用来初始化位置管理器和更新 View 的代码:

// ViewController.h =================
@property (strong, nonatomic) CLLocationManager *locationManager;

// ViewController.m =================
-(void)initBeaconMonitor {
    NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:estimoteBeaconUUID];
    _region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID major:estimoteBeaconMajor identifier:beaconRegionIdentifier];

    self.locationManager = [[CLLocationManager alloc] init];
    // For iOS 8
    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }

    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

    [self.locationManager startMonitoringForRegion:_region];
    [self.locationManager startRangingBeaconsInRegion:_region];
    [self.locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:
(NSArray *)beacons inRegion:(CLBeaconRegion *)region {

    if(beacons.count > 0) {
        CLBeacon *nearestBeacon = beacons.firstObject;

        if(nearestBeacon == _lastBeacon) {
            return;
        }

        _lastBeacon = [nearestBeacon copy];

        switch(nearestBeacon.proximity) {
            case CLProximityFar:
                break;
            case CLProximityNear:
                [self updateViewWithBeacon:nearestBeacon];
                break;
            case CLProximityImmediate:
                [self updateViewWithBeacon:nearestBeacon];
                break;
            case CLProximityUnknown:
                return;
        }
    }
}

最佳答案

您无法更改 iOS 上的信标扫描间隔。在前台测距时,iOS 会不断扫描信标,因此这不是造成延迟的原因。

问题在于,一旦 CoreLocation 确定信标处于附近/立即接近并且信标根据信标数组中的排序位置最接近,应用程序逻辑只会更新 UI。 proximity 字段的排序和值均基于 CoreLocation 到信标的距离估计(accuracy 字段),并且此基于移动设备和信标之间信号强度的 20 秒运行平均值。正是这 20 秒的运行平均值导致了您提到的延迟,因为距离估计更新缓慢并且在移动设备相对于所有信标处于同一位置 20 秒后才会达到稳定状态。 不幸的是,这 20 秒是固定的,不可配置。

为了更快的响应速度,您可以停止使用 accuracy 字段和数组中信标的排序顺序。一些备选方案:

  1. 将您的逻辑更改为完全不基于距离,可能基于时间和之前看到的信标。

  2. 切换到使用 rssi 字段作为距离的代理。该字段的平均时间仅为 1 秒,但请注意,由于 radio 噪声,它的变化也很大。该字段的负值越小表示信标越近。按照@heypiotr 在他的回答中建议的那样增加信标传输频率将有助于提供更稳定的 RSSI 数字。

  3. 在较短的时间间隔(2-5 秒)内手动计算您自己的 RSSI 运行平均值,以便在距离估计的快速更新和稳定性之间取得更好的权衡。

您可以在此处详细了解 iBeacon 测距的工作原理:

http://developer.radiusnetworks.com/2014/12/04/fundamentals-of-beacon-ranging.html

关于ios - iOS iBeacon 如何设置扫描间隔时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694841/

相关文章:

ios - 存档时无法识别快速桥接 header 中的框架导入

ios - Objective-c 中的协议(protocol)和接口(interface)有什么区别?

ios - 无法使用 ADLilyTableView

ios - XCode (git) 提交文件失败

iphone - 如何重新启动我的 iPhone 应用程序

objective-c - 你能通过 dismissModalViewControllerAnimated : or do you need a segue for the return journey? 返回数据吗

iphone - 使用 startMonitoringForRegion :region desiredAccuracy:accuracy? 可以监视的最大区域数是多少

ios - 如何捕获位置警报响应 iphone

ios - 即使在 iOS 11 中调用停止更新,bluebar 也会永远存在

ios - ScrollView 不在自动布局中滚动 subview