title: iBeacons ranging in background sometimes has delay
body: <p>I have an iBeacons app able to range for beacons while in background or not running. I implemented <code>UILocalNotifications</code> and they work fine, meaning i get a notification when i reach the range of a beacon.</p>
  
  <p>Not having a real beacon i created an app (for another device, let's say an <strong>iPad</strong> for the next scenario) that acts like 2 different beacons, meaning it can broadcast 2 different signals, same <code>UUID</code> but different <code>Major/Minor</code> values (call this beacon A and B), one at a time obviously. My problem is in this scenario:</p>
  
  <ol>
  <li>Have my iPhone (with iBeacons app closed) in lock screen</li>
  <li>Activate my <strong>iPad</strong> app, broadcasting beacon A</li>
  <li>My iPhone reacts showing me a notification </li>
  <li>I stop iPad app from broadcasting beacon A, wait 1 second, start broadcasting beacon B</li>
  <li>My iPhone <strong>DOESN'T</strong> react</li>
  <li>I stop iPad from broadcasting</li>
  <li>A few minutes later (about 2) my iPhone shows me the notification of beacon B</li>
  </ol>
  
  <p>Now what i don't understand is this delay, the first time my iPhone reacts immediately, the second time it takes about 2 minutes to notify me the beacon. </p>
  
  <p>If, after beacon B notification, i re-start broadcasting a beacon (A or B) my iPhone reacts immediately, then for the next time it always waits for 2 minutes. </p>
  
  <p>Why is this happening? I've read some article saying that it's because the bluetooth awakes every 2-4 minutes while the app is in background, so i can get the info not faster than this time. But i don't see much sense in this because whenever i get the <strong>second</strong> notification the broadcasting of the beacon (<strong>B</strong> in my scenario) was already <strong>stopped</strong>, it means that if the bluetooth awakes in that very moment no beacon was in the air! But i get the notification, so it means that in someway my iPhone found it before i stopped the broadcasting.</p>
  
  <p>Is this a problem that can be solved?</p>
  
  <p><strong>EDIT with some code</strong></p>
  
  <p>Here is my <code>viewDidLoad</code></p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Initialize location manager and set ourselves as the delegate and beacons dictionary
      _beacons = [[NSMutableDictionary alloc] init];
      self.locationManager = [[CLLocationManager alloc] init];
      self.locationManager.delegate = self;
  
      // Create a NSUUID with the same UUID as the broadcasting beacon
      NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"6C1AA496-1653-403D-BD1E-7F630AA6F254"];
  
      // Setup a new region with that UUID and same identifier as the broadcasting beacon
      self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                               identifier:@"testregion"];
  
      NSLog(@"startMonitoring");
      // Tell location manager to start monitoring for the beacon region
      [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
      [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
  
      _myBeaconRegion.notifyEntryStateOnDisplay = YES;
  
      // Check if beacon monitoring is available for this device
      if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]){
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; return;
      }
  
  }
  </code></pre>
  
  <p>Now whenever i get a beacon i send a notification, i just wanted to try how it worked so i didn't implemented yet a way to send just 1 notification, it means i get about 9 notification, 1 per sec that's the active time the code can run while in background i suppose (1 second enter region, 9 ranging for beacons)</p>
  
  <pre><code>-(void)locationManager:(CLLocationManager*)manager
         didRangeBeacons:(NSArray*)beacons
                inRegion:(CLBeaconRegion*)region
  {
      if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){
          UILocalNotification *notification = [[UILocalNotification alloc] init];
          notification.alertBody = @"Found Beacon";
          notification.soundName = @"Default";
          [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
      }
  }
  </code></pre>
  
  <p>Actually to be more specific, if i totally close my app from the multitasking view, or just let it in background, as soon as i start broadcasting a beacon i get the notification**S** (1 second delay). Then stopping the broadcasting and re-play the delay becomes in the order of minutes.</p>
  
  <p>Now for a real scenario, where i should have many beacons in the same place, this delay could be a problem, as long as i get the notification when i could be already far away from the beacons itself.</p>
  
  <p>Has my code any problem? I read those articles but i never found a delay of 15 minutes.</p>
  
  <p><strong>EDIT2 after davidgyoung suggestions</strong></p>
  
  <p>I modified my code and as you said using 2 different regions for beacon A and B the delay is always null. I also logged with the piece of code you gave and i discovered this.</p>
  
  <ol>
  <li>Broadcast the beacon of <code>Region_1</code></li>
  <li>Device shows me the notification of <code>Region_1</code></li>
  <li>Stop broadcasting the beacon of <code>Region_1</code></li>
  <li>The logs say i'm still in the region, only after a couple of minutes i get the log "OUTSIDE <code>Region_1</code>", just now i can re-play the broadcasting to get another notification from <code>Region_1</code>.</li>
  </ol>
  
  <p>So i was curious about this and i read and article <a href="http://beekn.net/2014/03/apple-ios-7-1-launches-major-ibeacon-improvement/" rel="nofollow">http://beekn.net/2014/03/apple-ios-7-1-launches-major-ibeacon-improvement/</a></p>
  
  <p>The author says that from iOS 7.1 the responsiveness of exiting a region is immediate, actually i'm running 7.1 but i also have a couple of minutes delay. Why this? Did you find the same problem in your tests?</p>
  
  <p>Now, i read that a device can listen for no more than 20 regions right?it means that if i have 100 beacons i can set up just 20 regions and divide these 100 in 20 groups, getting no more than 20 notification (assuming these 100 are in the same place, all in the range of my device) ? That could be a problem because will force the user to run the app in the foreground to get all the information (assuming again each of the 100 beacons have a particular and unit role), am i right?</p>