Search code examples
objective-cxcodeibeaconestimote

Beacon didRangeBeacons in Region not Called


My code was working perfectly some months ago. i dint chnage anything

I dont understand why the didRangeBeacons in Region not trigger when i launch my app :

#import "ESTViewController.h"
#import "PresentViewController.h"
#import <ESTBeaconManager.h>
#import <Parse/Parse.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ESTViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;

@property (nonatomic, strong) ESTBeacon* selectedBeacon;

@property (nonatomic, strong) NSArray *beaconsArray;

@end

@implementation ESTViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    // setup Estimote beacon manager.

    // Create the manager instance.
    self.beaconManager = [[ESTBeaconManager alloc] init];
    // Setup the delegate
    self.beaconManager.delegate = self;
    // Avoiding unknown state or not
    self.beaconManager.avoidUnknownStateBeacons = NO;
    // create sample region object (beacons in this case have all same uuid and same major)
    region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID identifier:@"multibeacons"];


    [self.beaconManager requestStateForRegion:region];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // start looking for estimote beacons in region
    // when beacon ranged beaconManager:didRangeBeacons:inRegion: invoked
    [self.beaconManager startRangingBeaconsInRegion:region];
    [self.beaconManager startMonitoringForRegion:region];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    // stop looking for estimote beacons in region
    [self.beaconManager stopRangingBeaconsInRegion:region];
    [self.beaconManager stopMonitoringForRegion:region];

}

#pragma mark - ESTBeaconManager delegate

    -(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    // descriptor on distance to sort the array of beacons by distance
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    // sorting the array of beacons
    // beacon array is sorted based on distance
    // closest beacon is the first one
    self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

    if([self.beaconsArray count] > 0)
    {

        if(!self.selectedBeacon)
        {
            // initialy pick closest beacon
            self.selectedBeacon = [beacons objectAtIndex:0];
            currentBeaconMinor = self.selectedBeacon.minor;
        }
        else
        {

            //sorting the array of beacons
           self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

           //updating the selected beacon with the first element of the array (closest beacon)
           if(self.selectedBeacon != [beacons objectAtIndex:0] )
            {
                self.selectedBeacon = [beacons objectAtIndex:0];
                currentBeaconMinor = self.selectedBeacon.minor;
            }

        }

        // Switch on proximity of the closest beacon
        switch (self.selectedBeacon.proximity)
        {
            case CLProximityUnknown:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"lost.jpg"];
                self.rangeStatusImageView.hidden = NO;
                self.signalStatusImageView.hidden = YES;
                self.descriptionStateLabel.text = @"Recherche de signal ... Si le problème persiste, contactez l'accueil";

                [UIView animateWithDuration:1.0

                                      delay: 0.0

                                    options: UIViewAnimationOptionCurveEaseIn

                                 animations:^{

                                     self.rangeStatusImageView.alpha = 0.3;

                                 }

                                 completion:^(BOOL finished){


                                     [UIView animateWithDuration:1.0

                                                           delay: 0.0

                                                         options:UIViewAnimationOptionCurveEaseOut

                                                      animations:^{

                                                          self.rangeStatusImageView.alpha = 1.0;

                                                      }

                                                      completion:nil];

                                 }];

                break;
            }
            case CLProximityImmediate:
            {

                if ([currentBeaconMinor floatValue] == 128)
                {
                    NSLog(@"128 128 128");
                    [self performSegueWithIdentifier: @"presentSegue1" sender: self];
                }

                else if ([currentBeaconMinor floatValue] == 228)
                {
                    NSLog(@"228 228 228");
                    [self performSegueWithIdentifier: @"presentSegue2" sender: self];
                }
                else if ([currentBeaconMinor floatValue] == 328)
                {
                    NSLog(@"328 328 328");
                    [self performSegueWithIdentifier: @"presentSegue3" sender: self];
                }

                break;
            }
            case CLProximityNear:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"near.jpg"];
                self.signalStatusImageView.hidden = NO;

                AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

                [UIView animateWithDuration:1.0

                                      delay: 0.0

                                    options: UIViewAnimationOptionCurveEaseIn

                                 animations:^{

                                     self.rangeStatusImageView.alpha = 0.3;

                                 }

                                 completion:^(BOOL finished){


                                     [UIView animateWithDuration:1.0

                                                           delay: 0.0

                                                         options:UIViewAnimationOptionCurveEaseOut

                                                      animations:^{

                                                          self.rangeStatusImageView.alpha = 1.0;

                                                      }

                                                      completion:nil];

                                 }];


                self.rangeStatusImageView.hidden = NO;
                self.descriptionStateLabel.font = [UIFont systemFontOfSize:17];
                self.descriptionStateLabel.text = @"Approchez vous d'un article pour obtenir plus d'informations";
                break;

            }
            case CLProximityFar:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"far.jpg"];
                [self.rangeStatusImageView stopAnimating];
                self.rangeStatusImageView.hidden = NO;
                self.signalStatusImageView.hidden = NO;
                self.descriptionStateLabel.text = @"Bienvenue dans notre ... ";
                break;
            }

            default:
                break;

        }
        self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];
    }
}

i have a break point at the line after the pragma mark , it never reach that code. I really dont understand why.

Thanks for your help.


Solution

  • I found a solution :

    i updated the Info.plist file with :

    <key>NSLocationAlwaysUsageDescription</key>
    <string>This application monitors your location to show you promotional offers in shops you're passing by.</string>
    

    In the iphone i have change the settings for localisation for my app to "always"