iBeacon:获得主要和次要 - 只寻找uuid

Mat*_*hew 7 iphone ibeacon

我正在使用air locate示例并仅通过uuid监视iBeacons.当我得到输入的区域事件时,如果我只是在寻找uuid,我似乎无法从触发事件的信标/区域获得主要和次要(如果我正在监视一个uuid,我可以指定的主要和次要) - 有没有人知道这样做的方法/我错过了什么?

我真的不想开始测距 - 看起来我不应该......

(用例是说许多商店都带有相同uuid的信标,然后发出操作系统通知,其中包含有关该商店的相关信息(通过查询主要和次要获得))

这基本上就是我做的事情:

CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"blah"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
region.notifyEntryStateOnDisplay = YES;

[self.locationManager startMonitoringForRegion:region];
Run Code Online (Sandbox Code Playgroud)

然后在app委托中:

- (void) locationManager:(CLocationManager*)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion*)region {

    // assume for now its the iBeacon
    CLBeaconRegion *beaconRegion = (CLBeaconRegion*) region;

    beaconRegion.major  // hasn't been set...

}
Run Code Online (Sandbox Code Playgroud)

非常感谢!

dav*_*ung 20

你没有做错任何事.看起来令人惊讶,监控API没有为您提供触发区域更改的特定信标.

未在CLBeaconRegion对象上设置major的原因是因为它与您用于开始监视的对象完全相同,并且您将该字段设置为nil(或者根本没有将其设置为nil).您正在寻找的是一个额外的CLBeacon对象数组.正如您所建议的那样,这仅存在于范围API上.

开始测量并不是什么大不了的事.只需在开始监控的同时进行设置:

CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"blah"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
region.notifyEntryStateOnDisplay = YES;

[self.locationManager startMonitoringForRegion:region]; 
[self.locationManager startRangingBeaconsInRegion:region]; 
Run Code Online (Sandbox Code Playgroud)

如果您只关心第一个范围调用,则可以使用标志忽略进一步的更新:

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    if (!_firstOneSeen) { 
        // Do something with beacons array here
    }
}
Run Code Online (Sandbox Code Playgroud)

离开该地区时重置该标志

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    _firstOneSeen = NO;
}
Run Code Online (Sandbox Code Playgroud)

作为奖励,当您的应用程序处于前台时,这也将使您的监控响应时间更快.请参阅:http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html

  • 好帖子.然而,一个小的修正.您在didEnterRegion/didExitRegion调用中获得的region对象与您用于注册的CLBeaconRegion对象不同 - 它是一个副本.我知道这一点,因为起初我尝试使用表单if(region == regionImTracking)的代码并且比较失败.我记录了两个地区,他们的地址不同,而他们的所有设置都匹配. (3认同)