0
votes

I am trying to get the "major" and "minor" id from the beacon that triggers a didEnterRegion method. I have been told that I can do this by combining ranging and monitoring together, but I cant seem to get it working right.

I am using the Estimote beacons and am using the Estimote API. Any ideas whats going wrong here? Thanks!

Here's a link to where it says you can combine monitoring and ranging: iBeacon: get major and minor - only looking for uuid

Setup:

#import "ViewController.h"
#import "ESTBeaconManager.h"

@interface ViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) UIImageView*      bgImageView;
@property (nonatomic, assign) BOOL              notificationShown;
@property (nonatomic, strong) UIImageView*      productImage;

@end

@implementation ViewController

ViewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beaconManager.avoidUnknownStateBeacons = YES;

    ESTBeaconRegion* region = [[ESTBeaconRegion alloc] 
    initRegionWithIdentifier:@"EstimoteSampleRegion"];

    [self.beaconManager startMonitoringForRegion:region];
    [self.beaconManager requestStateForRegion:region];
    [self.beaconManager startRangingBeaconsInRegion:region];

    [[NSUserDefaults standardUserDefaults] setObject:@"FALSE" 
    forKey:@"connectedToBeacon"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

DidRangeBeacons:

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region {

    NSString *connectedToBeacon = [[NSUserDefaults standardUserDefaults] 
    stringForKey:@"connectedToBeacon"];

    if (connectedToBeacon == FALSE) {

        NSNumber *beaconMajor = region.major;
        NSNumber *beaconMinor = region.minor;

        NSString *alertText = [NSString stringWithFormat:@" Entering (%@,%@)", 
        beaconMajor, beaconMinor];

        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = alertText;
        notification.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

        [[NSUserDefaults standardUserDefaults] setObject:@"TRUE" 
        forKey:@"connectedToBeacon"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
}
1
Just a note, the local notification is not even firing at all when I enter the region :)user2492064
I would add two NSLog statements, one at the top of the didRangeBeacons method, and the other inside the if statement. Do you see either or both log lines?davidgyoung
To clarify what I am getting at in the above question, do you know for sure the ranging is working at all? Is your beacon working? Testing with hese log statements will help you find out.davidgyoung
Got it to work David, thank you!user2492064
Good to hear! If you have a chance, please document what the issue was here so it might help others with a similar problem.davidgyoung

1 Answers

0
votes

I haven't yet tried using Estimote's frameworks. I've been using the Core Location framework's Core Location manager, CLBeaconRegion and CLBeacon classes, so my answer is going to be based on that.

Assuming they work the same way, the beacon ranging call passes you both an array of 1 or more beacons and the region they match.

The major and minor version values in the region will be nil unless you set up the regions with those values.

The major and minor values in the beacon objects, however, will contain the major and minor numbers of the beacons you've actually detected. If you are currently detecting more than one you have to come up with logic that picks one. What I've done is to loop through and select the closest one (using accuracy) who's proximity is not unknown.

(EDITED to correct a few typos)