IBeacon main and minor value inside didEnterRegion
I am trying to access the major and minor values for the closest beacon inside a didEnterRegion delegate. However, when printing the values to the console, they return null
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]]) {
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
int major = [beaconRegion.major intValue];
int minor = [beaconRegion.minor intValue];
NSLog(@" Major %@ Minor %@", beaconRegion.major, beaconRegion.minor);
}
}
source to share
The area monitoring callback you run will not tell you the individual beacon IDs you find. If you want to get ids for individual beacons, you should use APIs to define the range of beacons, as @Larme says in his comment. The ranking callback includes a second parameter, which is an array of all visible beacons.
source to share
It looks like you are not initializing the BeaconRegion with insignificant and significant values
When initializing the beacon area, you need to use
initWithProximityUUID:major:minor:identifier:
instead
initWithProximityUUID:identifier:
If you don't want to initialize minor and major values across regions, you can call the didRangeBeacons method as mentioned in the comments.
source to share
you are trying to get the major and minor values of the region. but you say you want beacon values.
it depends on which brand beacon you are using, but there must be a method that returns the array of beacons found by the device. usually the first object in the array is the closest one. in this method you can get beacon values.
example code:
- (void)beaconManager:(ESTBeaconManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
{
if([beacons count] > 0)
{
// beacon array is sorted based on distance
// closest beacon is the first one
ESTBeacon* closestBeacon = [beacons objectAtIndex:0];
NSString* theMajor = [NSString stringWithFormat:@"%@",closestBeacon.major];
}
}
source to share
Setting up locationManager (don't forget to set up the plist for iOS 8 to add NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription to these 2 values).
@property (strong, nonatomic) CLLocationManager *locationManager;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Needed for iOS 8
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
Then call startRangingItem:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
if ([region isKindOfClass:[CLBeaconRegion class]]) {
[self startRangingItem];
}
}
- (void)startRangingItem {
CLBeaconRegion *beaconRegion = [self beaconRegionWithItem];
[self.locationManager startRangingBeaconsInRegion:beaconRegion];
}
- (CLBeaconRegion *)beaconRegionWithItem{
NSUUID *iPadTransmitterUUID = [[NSUUID alloc] initWithUUIDString:@"AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEFFFFF1"];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:iPadTransmitterUUID identifier:@"Transmitter1"];
return beaconRegion;
}
Then in didRangeBeacons: ***
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
CLBeacon *testBeacon = [beacons objectAtIndex:0];
NSLog(@"Inside didRangeBeacons Major %@ Minor %@", testBeacon.major, testBeacon.minor);
}
*** Please note that this didRangeBeacons method will only run in the background for 5 seconds after the user logs into the region. After 5 seconds, it will stop. If you want to proceed with the configuration, the user needs to launch the application. (forcing didRangeBeacons to run in the background is possible, but it could be rejected by apple)
source to share