I m a beginner in objective C. My app work correctly with one beacon. I'm using the "estimote SDK". I have many problems, i want to use 2 or 3 beacons. I want to push a View for each of the beacons.
I don't understand how i can do it with multiple beacons.
I don't know if i have to use multiple beacon manager. (ESTBeaconManager* beaconManager)
I dont know how to pass differents regions to the didRangeBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
Can i use one beacon only for notification and the 2 others to pop 2 differents view when i m close of them. ( one different view for each beacon )
Thanks for your help.
Best Regards.
Code:
#import "ESTViewController.h"
#import "PresentViewController.h"
#import <ESTBeaconManager.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ESTViewController () <ESTBeaconManagerDelegate>
@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) ESTBeaconManager* beaconManager2;
@property (nonatomic, strong) ESTBeaconManager* beaconManager3;
@property (nonatomic, strong) ESTBeacon* selectedBeacon;
@end
@implementation ESTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// should i create one manager instance or more ?
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;
self.beaconManager.avoidUnknownStateBeacons = NO;
//self.beaconManager2 = [[ESTBeaconManager alloc] init];
//self.beaconManager2.delegate = self;
//self.beaconManager2.avoidUnknownStateBeacons = NO;
//self.beaconManager3 = [[ESTBeaconManager alloc] init];
//self.beaconManager3.delegate = self;
//self.beaconManager3.avoidUnknownStateBeacons = NO;
// My Differents regions
region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
major:12800 minor:228 identifier:@"Icy Marshmellow"];
region2 = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
major:12800 minor:128 identifier:@"Mint Cocktail"];
region3 = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
major:12800 minor:328 identifier:@"Blueberry Pie"];
// Should i do it for each region with one ESTBeaconManager or 3 ?
[self.beaconManager requestStateForRegion:region];
[self.beaconManager requestStateForRegion:region2];
[self.beaconManager requestStateForRegion:region3];
}
// NOTIFICATION METHOD :
-(void)beaconManager:(ESTBeaconManager *)manager
didEnterRegion:(ESTBeaconRegion *)region
{
// iPhone/iPad entered beacon zone
// present local notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Hello blabla blabla";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)beaconManager:(ESTBeaconManager *)manager
didExitRegion:(ESTBeaconRegion *)region
{
// iPhone/iPad left beacon zone
// present local notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"bye bye";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.beaconManager startRangingBeaconsInRegion:region];
[self.beaconManager startMonitoringForRegion:region];
//[self.beaconManager2 startRangingBeaconsInRegion:region2];
//[self.beaconManager2 startMonitoringForRegion:region2];
//[self.beaconManager3 startRangingBeaconsInRegion:region3];
//[self.beaconManager3 startMonitoringForRegion:region3];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.beaconManager stopRangingBeaconsInRegion:region];
[self.beaconManager stopMonitoringForRegion:region];
//[self.beaconManager2 stopRangingBeaconsInRegion:region2];
//[self.beaconManager2 stopMonitoringForRegion:region2];
//[self.beaconManager3 stopRangingBeaconsInRegion:region3];
//[self.beaconManager3 stopMonitoringForRegion:region3];
}
// My problem is here , i dont know how i can pass differents regions here
-(void)beaconManager:(ESTBeaconManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(ESTBeaconRegion *)region
{
if([beacons count] > 0)
{
if(!self.selectedBeacon)
{
// initialy pick closest beacon
self.selectedBeacon = [beacons objectAtIndex:0];
}
else
{
for (ESTBeacon* cBeacon in beacons)
{
// update beacon it same as selected initially
if([self.selectedBeacon.major unsignedShortValue] == [cBeacon.major unsignedShortValue] &&
[self.selectedBeacon.minor unsignedShortValue] == [cBeacon.minor unsignedShortValue])
{
self.selectedBeacon = cBeacon;
}
}
}
switch (self.selectedBeacon.proximity)
{
case CLProximityUnknown:
{
self.rangeStatusImageView.image = [UIImage imageNamed:@"logo_signal.jpg"];
self.descriptionStateLabel.text = @"Signal lost";
break;
}
case CLProximityImmediate:
{
[self performSegueWithIdentifier: @"presentSegue" sender: self];
break;
}
case CLProximityNear:
{
self.rangeStatusImageView.image = [UIImage imageNamed:@"logo_near_bleu.jpg"];
self.descriptionStateLabel.text = @"Come closer";
break;
}
case CLProximityFar:
{
self.rangeStatusImageView.image = [UIImage imageNamed:@"logo_far_clair.jpg"];
self.descriptionStateLabel.text = @"Welcome";
break;
}
default:
break;
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
EDIT
Ok i worked on my code and now i do it with one region. my array of beacons have 3 beacons.
region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID identifier:@"multibeacons"];
i dont use major or minor at the init.
in ViewDidAppears i do :
[self.beaconManager startRangingBeaconsInRegion:region];
The delegate like this :
-(void)beaconManager:(ESTBeaconManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(ESTBeaconRegion *)region
{
// I used a sort , sorting by distance
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
// if breakpoint here 3 beacons in array
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
{
for (ESTBeacon* cBeacon in self.beaconsArray)
{
// update beacon it same as selected initially
if([self.selectedBeacon.major unsignedShortValue] == [cBeacon.major unsignedShortValue] &&
[self.selectedBeacon.minor unsignedShortValue] == [cBeacon.minor unsignedShortValue])
{
self.selectedBeacon = cBeacon;
currentBeaconMinor = self.selectedBeacon.minor;
}
}
}
I sort by distance and i have a currentBeaconMinor Value. My array of beacon have 3 beacons inside if i put a breakpoint i can see 3.
In the Switch proximity , i have do like this :
switch (self.selectedBeacon.proximity)
{
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;
}
But that still dont work :((( I m getting mad. My app pick initialy the closest beacon. After that app always keep the same beacon and never change. I move the beacon near the device but nslog always send me the same minor number. Please can you give me some help ? I m sure i m doing something wrong.