سؤال

I'm trying to use CoreLocation to get multiple iBeacons as specified by my iOS app- the idea is that if they're in range, it'll notify me for each one as it finds them.

The problem is that in the didEnterRegion and didExitRegion methods, I have to provide a CLBeaconRegion object. There's one of these for every iBeacon, and if I was just using one iBeacon, I could just use that one, but since there are several, I need to know how to find each CLBeaconRegion from those methods.

Maybe I'm misunderstanding how this works; if so, please let me know.

- (void)getForUUUIDs:(CDVInvokedUrlCommand *)command
{
        //Get an array of UUID's to filter by
        self->locationUUIDs = [self getArgsObject:command.arguments];

        self->locationManager = [[CLLocationManager alloc] init];
        self->locationManager.delegate = self;
        scanCallback = command.callbackId;

        for(NSInteger i = 0; i < [locationUUIDs count]; i++) {
            NSString *identifier = [NSString stringWithFormat:@"BLERegion %d",i];
            CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[locationUUIDs  allKeys] objectAtIndex:i] identifier:identifier];
            [self->locationManager startMonitoringForRegion:thisRegion];
        }
}

    - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
    [self->locationManager startRangingBeaconsInRegion:????];
}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
    [self->locationManager stopRangingBeaconsInRegion:????];
}
هل كانت مفيدة؟

المحلول

Ranging on the same region that fired the monitor entry/exit event is extremely simple:

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
    [self->locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region];
}

This will start ranging on the exact same region you used to start monitoring. Note that there is a region parameter passed to the callback. That Region parameter will include the same UUID that you set up before.

One other point: while there is nothing wrong with starting ranging when you enter a region and stopping ranging when you exit a region, there is really no need to do this. Just start ranging the same time you start monitoring. Because ranging won't do anything when the iBeacon isn't visible, the end result will be almost identical. The only difference is you will probably get your first ranging callback one second sooner if you set it up ahead of time. There is no extra drain on battery or system resources.

The added benefit of setting it up ahead of time is that you don't have to do the casting of the CLRegion object -- you have the original object to begin with. And you don't have to implement the monitoring callback methods, so your code is simpler. Like this:

- (void)getForUUUIDs:(CDVInvokedUrlCommand *)command
{
    //Get an array of UUID's to filter by
    self->locationUUIDs = [self getArgsObject:command.arguments];

    self->locationManager = [[CLLocationManager alloc] init];
    self->locationManager.delegate = self;
    scanCallback = command.callbackId;

    for(NSInteger i = 0; i < [locationUUIDs count]; i++) {
        NSString *identifier = [NSString stringWithFormat:@"BLERegion %d",i];
        CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[locationUUIDs  allKeys] objectAtIndex:i] identifier:identifier];
        [self->locationManager startMonitoringForRegion:thisRegion];
        [self->locationManager startRangingBeaconsInRegion:thisRegion];
    }
}

نصائح أخرى

your region is specified by a uuid

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                       identifier:identifier];

All your beacons must share that uuid. So when you range your beacons, you can get them in that method (CLLocationManagerDelegate).

    -(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
    for (CLBeacon *beacon in beacons) {
        NSLog(@"Major : %@", beacon.major);
        NSLog(@"Minor : %@", beacon.minor);
    }
}

The attributes major and minor are here to differentiate your beacons.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top