1
votes

I have created a iphone app for beacons. In that i want to display the message when i am exit from all the beacons region.

  1. I dont want to display the message for every beacon exit region. For example, If have 3 beacons, I want to display the message only when i am exit of all the 3 beacons. Is it possible to do that?

  2. And also i want to get the exiting beacon major and minor values in didExitRegion

I used the following code:

-(void)locationManager:(CLLocationManager*)manager
   didRangeBeacons:(NSArray*)beacons
          inRegion:(CLBeaconRegion*)region
{
// Beacon found!
NSLog(@"iBeacons found");
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Successfully found" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show];

CLBeacon *foundBeacon = [beacons firstObject];

// You can retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *majorId = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minorId = [NSString stringWithFormat:@"%@", foundBeacon.minor];
NSLog(@"UUID: %@", uuid);
}

In the above code i can get the uuid, major, minor of the beacons. But I want to get the values of the exiting beacon in didExitRegion. Is it possible?

Thanks in advance.

2

2 Answers

0
votes

Use a mutable array to keep track of the region details of the detected beacon. And update that array with detection of new beacon regions. Like the code below

In delegate method didDetermineState add

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside)
    {
        [regions addObject:region]; // regions is the mutable array 
    }
}

And with didExitRegion method add

// Tells the delegate that the user left the specified region. 
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
        [regions removeObject:region];
        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
        NSLog(@"\nExited region id: %@",beaconRegion.identifier);
        NSLog(@"\nExited region major: %@",beaconRegion.major);
        NSLog(@"\nExited region minor: %@",beaconRegion.minor);

       // Add a check with regions array here to show the custom alert message

    }
0
votes

I dont want to display the message for every beacon exit region. For example, If have 3 beacons, I want to display the message only when i am exit of all the 3 beacons. Is it possible to do that?

Your requirements don't seem completely clear. If these three beacons are described by a single region you're monitoring, then it's simple: you will only get a single didExitRegion: message as you leave the range of the last beacon in the region. Otherwise, Alex's suggestion above seems reasonable: track regions you're entering and exiting, then conditionally execute some code in didExitRegion: when the count of regions you're currently in drops to zero.

And also i want to get the exiting beacon major and minor values in didExitRegion

I'm not sure "exiting beacon" is well-defined here (do you mean, say, the last beacon in the region whose range you left, hence triggering the didExitRegion: message?), but in any case you can't get what you're asking for here. Where Alex logs major and minor, they are attributes of the region being monitored (and, in this case, being exited), not of any specific beacon.