如何在iOS上为iBeacon设置扫描间隔时间?

时间:2015-04-17 09:04:57

标签: ios objective-c core-location ibeacon estimote

我正在开发支持iBeacon的应用。基本上,我有一个视图根据最近的信标更新其内容,并扫描信标,我没有使用框架(只是Apple的CoreLocation),即使我使用Estimote Beacons。

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

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

以下是我用来初始化位置管理器和更新视图的代码:

// 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;
        }
    }
}

3 个答案:

答案 0 :(得分:11)

您无法在iOS上更改信标扫描间隔。在前景中进行测距时,iOS会不断扫描信标,因此这不会导致延迟。

问题在于,只有CoreLocation确定信标处于近/近接近状态并且信标根据信标阵列中的排序位置最近,app逻辑才会更新UI。 proximity字段的排序和值都基于CoreLocation对信标的距离估计(accuracy字段),这是基于20秒移动设备和信标之间的信号强度的平均值。正是这20秒的运行平均值导致了你提到的延迟,因为距离估计缓慢更新并且直到移动设备相对于所有信标在相同位置持续20秒才达到稳定状态。 / strong>不幸的是,这20秒是固定的,不可配置。

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

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

  2. 切换到使用rssi字段作为距离的代理。此字段的平均值仅为1秒,但请注意,如果由于无线电噪声引起的可变性,它会有很多。该字段的较小负值表示更接近的信标。如@heypiotr在答案中建议的那样,增加信标传输频率将有助于提供更稳定的RSSI数字。

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

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

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

答案 1 :(得分:2)

减少信标的广告间隔应该可以提高响应速度,请记住它是以电池寿命为代价的。大约300-400毫秒的东西通常是前者和后者之间的良好平衡。您可以使用Estimote's iOS app配置信标。

iOS使用信标的广告包来估计到信标的距离,从而找出邻近区域(远/近/立即)。它通常需要收集至少几个数据包,以确保估计值或多或少正确。这就是为什么频率较高的广告应该有所帮助。

答案 2 :(得分:0)

您也可以像普通BLE设备一样启用CoreBluetooth并扫描您的信标。 这样做时,您可以使用例如计时器强制刷新蓝牙扫描。 请注意,此解决方案不适用于后台,并且不是非常节能。