我正在开发一个支持 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
字段和数组中信标的排序顺序。一些备选方案:
将您的逻辑更改为完全不基于距离,可能基于时间和之前看到的信标。
切换到使用
rssi
字段作为距离的代理。该字段的平均时间仅为 1 秒,但请注意,由于 radio 噪声,它的变化也很大。该字段的负值越小表示信标越近。按照@heypiotr 在他的回答中建议的那样增加信标传输频率将有助于提供更稳定的 RSSI 数字。在较短的时间间隔(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/