iBeacon进入区域时的major和minor值

4
我正在尝试在didEnterRegion代理中访问最近信标的主要值和次要值。然而,当将这些值打印到控制台时,它们返回为null。
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {

    if ([region isKindOfClass:[CLBeaconRegion class]]) {

        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;

        int major = [beaconRegion.major intValue];
        int minor = [beaconRegion.minor intValue];

        NSLog(@" Major %@ Minor %@", beaconRegion.major, beaconRegion.minor);

        }
}

5
locationManager:didEnterRegion代理方法中,执行[yourLocationManager startRangingBeaconsInRegion:yourRegion];,对应的委托方法是locationManager:didRangeBeacons:inRegion: - Larme
5个回答

6
您所实现的区域监控回调函数将不会告诉您检测到的信标的个别标识符。如果您想获取检测到的个别信标的标识符,则需要使用信标定距 API,正如 @Larme 在他的评论中所说。用于定距的回调包括第二个参数,该参数是所有被检测到的信标的数组。

2
你需要区分监控测距的iBeacons。只有成功测距的iBeacons才能为您提供主/次要ID。

0

配置locationManager(记得在iOS 8中配置plist以添加这两个值NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription)。

@property (strong, nonatomic) CLLocationManager *locationManager;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

[[UIApplication sharedApplication] cancelAllLocalNotifications];

// Needed for iOS 8
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

然后调用 startRangingItem:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    if ([region isKindOfClass:[CLBeaconRegion class]]) {

        [self startRangingItem];
    }
}


- (void)startRangingItem {
    CLBeaconRegion *beaconRegion = [self beaconRegionWithItem];
    [self.locationManager startRangingBeaconsInRegion:beaconRegion];
}

- (CLBeaconRegion *)beaconRegionWithItem{

    NSUUID *iPadTransmitterUUID = [[NSUUID alloc] initWithUUIDString:@"AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEFFFFF1"];

    CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:iPadTransmitterUUID identifier:@"Transmitter1"];

    return beaconRegion;
}

然后在didRangeBeacons方法中:***

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

    CLBeacon *testBeacon = [beacons objectAtIndex:0];

    NSLog(@"Inside didRangeBeacons Major %@ Minor %@", testBeacon.major, testBeacon.minor);

}

请注意,didRangeBeacons方法只会在用户进入区域后在后台运行5秒钟。5秒后它将停止搜索。如果您想继续搜索,用户需要启动应用程序。(强制在后台运行didRangeBeacons是可能的,但可能会被苹果拒绝)

0

你正在尝试获取区域的主要和次要值。但是你说想获取信标的值。

这取决于你使用的信标品牌,但必须有一个方法返回设备发现的信标数组。通常,数组的第一个对象是最接近的信标。 在该方法中,你可以获取信标的值。

示例代码:

- (void)beaconManager:(ESTBeaconManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
{
    if([beacons count] > 0)
    {
        // beacon array is sorted based on distance
        // closest beacon is the first one
        ESTBeacon* closestBeacon = [beacons objectAtIndex:0];

        NSString* theMajor = [NSString stringWithFormat:@"%@",closestBeacon.major];
    }

}

0

看起来你没有使用次要值和主要值初始化BeaconRegion

在初始化beacon区域时,您需要使用 initWithProximityUUID:major:minor:identifier:

而不是

initWithProximityUUID:identifier:

如果您不想将次要值和主要值初始化到区域中,则可以按照评论中提到的调用didRangeBeacons方法。


这个方案是可行的,但需要注意iOS将每个应用程序沙盒限制为最多20个区域。如果您使用major和minor初始化beacon区域,则很快就会达到可以使用的beacon数量上限。如果您只使用UUID初始化beacon区域,则可以访问超过20个beacon(例如,每个区域包含多个beacon)。 - kevinl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接