I have been playing around with iBeacons for a few weeks now and was woundering if it was possible to monitor two beacons from the same view controller.
For example so far I have made an app with three sperate views that each react to a seperate beacon changing content dependent on range and triggering dynamic content like music and video. All these view are the same layout so I was woundering if I could change it so I had one view controller that changed the content dependent on what beacon I am near rather than having to change the view.
This would make more sense as you can only monitor one which means when the segue triggers and the new beacon is being monitored if you move back towards the first beacon it doesn't chnage back to that view when you enter the first beacons proximity.
I'm sure there is a way to do this as beacons are being used in apple store in america to trigger promotion, etc and they changhe dependednt on the closest beacon with no problem.
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ExhibitsViewController : UIViewController <CLLocationManagerDelegate,UIScrollViewDelegate>
{
CLBeacon *beacon;
}
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager
@end
This is the code in my header file that is defining the beacon, beaconRegion and locationManager.
#import "MultipleBeaconsViewController.h"
@interface MultipleBeaconsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rangeLabel;
@property (weak, nonatomic) IBOutlet UILabel *uuidLabel;
@end
@implementation MultipleBeaconsViewController
NSUUID *iBeacon1uuid;
NSUUID *iBeacon2uuid;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self initRegion];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.iBeacon1Region];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.iBeacon2Region];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)initRegion {
iBeacon1uuid = [[NSUUID alloc] initWithUUIDString:@"11111111-2222-3333-4444-555555555555"];
_iBeacon1Region = [[CLBeaconRegion alloc] initWithProximityUUID:iBeacon1uuid identifier:@"com.private.Gallery"];
_iBeacon1Region.notifyOnEntry = YES;
_iBeacon1Region.notifyOnExit = YES;
// launch app when display is turned on and inside region
_iBeacon1Region.notifyEntryStateOnDisplay = YES;
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[_locationManager startMonitoringForRegion:_iBeacon1Region];
[_locationManager startRangingBeaconsInRegion:_iBeacon1Region];
}
iBeacon2uuid = [[NSUUID alloc] initWithUUIDString:@"55555555-4444-3333-2222-111111111111"];
_iBeacon2Region = [[CLBeaconRegion alloc] initWithProximityUUID:iBeacon2uuid identifier:@"com.private.Gallery"];
_iBeacon2Region.notifyOnEntry = YES;
_iBeacon2Region.notifyOnExit = YES;
// launch app when display is turned on and inside region
_iBeacon2Region.notifyEntryStateOnDisplay = YES;
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[_locationManager startMonitoringForRegion:_iBeacon2Region];
[_locationManager startRangingBeaconsInRegion:_iBeacon2Region];
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]])
{
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
if ([beaconRegion.proximityUUID isEqual: iBeacon1uuid])
{
_titleLabel.text = @"Beacon 1 proximity entered";
}
else if ([beaconRegion.proximityUUID isEqual: iBeacon2uuid])
{
_titleLabel.text = @"Beacon 2 proximity entered";
}
}
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
if ([region isKindOfClass:[CLBeaconRegion class]]) {
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
if ([beaconRegion.proximityUUID isEqual: iBeacon1uuid])
{
_titleLabel.text = @"Beacon 1 proximity exited";
}
else if ([beaconRegion.proximityUUID isEqual: iBeacon2uuid])
{
_titleLabel.text = @"Beacon 2 proximity exited";
}
}
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ([region isKindOfClass:[CLBeaconRegion class]]) {
for (CLBeacon *beacon in beacons)
{
NSString *beaconID = [beacons objectAtIndex:0];
NSLog(@"%@",beaconID);
if ([region.proximityUUID isEqual:iBeacon1uuid])
{
if (beacon.accuracy >=0.000001 && beacon.accuracy <=0.500000)
{
_titleLabel.text = @"Beacon 1";
_rangeLabel.text = [NSString stringWithFormat:@"%f",beacon.accuracy];
_uuidLabel.text = [NSString stringWithFormat:@"%@", beacon.proximityUUID];
}
}
else if ([region.proximityUUID isEqual:iBeacon2uuid])
{
if (beacon.accuracy >=0.000001 && beacon.accuracy <=0.500000) {
_titleLabel.text = @"Beacon 2";
_rangeLabel.text = [NSString stringWithFormat:@"%f",beacon.accuracy];
_uuidLabel.text = [NSString stringWithFormat:@"%@", beacon.proximityUUID];
}
else
{
}
}
}
}
}
This is my code in the implementation file that is ranging the beacon.