diff --git a/Discovery/Discovery.h b/Discovery/Discovery.h index 724f5f5..6ae8745 100644 --- a/Discovery/Discovery.h +++ b/Discovery/Discovery.h @@ -12,12 +12,32 @@ #import #import "BLEUser.h" +/** Start options */ +typedef NS_ENUM(NSInteger, DIStartOptions) { + DIStartAdvertisingAndDetecting = 0, + DIStartAdvertisingOnly, + DIStartDetectingOnly, + DIStartNone +}; + @interface Discovery : NSObject /** * Initialize the Discovery object with a UUID specific to your app, and a username specific to your device. - * The usersBlock is triggered periodically in order of users' proximity. It + * The usersBlock is triggered periodically in order of users' proximity. + * The startOptions determine if the beacon should start advertising, broadcasting, both, or none. + */ +- (instancetype)initWithUUID:(CBUUID *)uuid + username:(NSString *)username + startOption:(DIStartOptions)startOption + usersBlock:(void (^)(NSArray *users, BOOL usersChanged))usersBlock; + + +/** + * Initialize the Discovery object with a UUID specific to your app, and a username specific to your device. + * The usersBlock is triggered periodically in order of users' proximity. + * The Discovery object starts both advertising and detecting. */ - (instancetype)initWithUUID:(CBUUID *)uuid username:(NSString *)username @@ -28,6 +48,12 @@ */ - (BLEUser *)userWithPeripheralId:(NSString *)peripheralId; +/** + * Changing these properties will start/stop advertising/discovery + */ +@property (nonatomic) BOOL shouldAdvertise; +@property (nonatomic) BOOL shouldDiscover; + /** * UUID is used for id as advertisement, peripheral services and characteristics. * It should be unique to you app, not to your device. Otherwise the peers won't be able to discover each other. diff --git a/Discovery/Discovery.m b/Discovery/Discovery.m index e1c1b17..3a644e0 100644 --- a/Discovery/Discovery.m +++ b/Discovery/Discovery.m @@ -14,8 +14,10 @@ @interface Discovery() @end @implementation Discovery + - (instancetype)initWithUUID:(CBUUID *)uuid - username:(NSString *)username + username:(NSString *)username + startOption:(DIStartOptions)startOption usersBlock:(void (^)(NSArray *users, BOOL usersChanged))usersBlock { self = [super init]; if(self) { @@ -28,6 +30,8 @@ - (instancetype)initWithUUID:(CBUUID *)uuid _userTimeoutInterval = 3; _updateInterval = 2; + + // listen for UIApplicationDidEnterBackgroundNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) @@ -47,15 +51,77 @@ - (instancetype)initWithUUID:(CBUUID *)uuid // start the central and peripheral managers self.queue = dispatch_queue_create("com.omerfarukgul.discovery", DISPATCH_QUEUE_SERIAL); - self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.queue]; - self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.queue]; + _shouldAdvertise = NO; + _shouldDiscover = NO; + + switch (startOption) { + case DIStartAdvertisingAndDetecting: + self.shouldAdvertise = YES; + self.shouldDiscover = YES; + break; + case DIStartAdvertisingOnly: + self.shouldAdvertise = YES; + break; + case DIStartDetectingOnly: + self.shouldDiscover = YES; + break; + case DIStartNone: + default: + break; + } - [self startTimer]; } return self; } +-(void)setShouldAdvertise:(BOOL)shouldAdvertise { + if(_shouldAdvertise == shouldAdvertise) + return; + + _shouldAdvertise = shouldAdvertise; + + if(shouldAdvertise) { + if (!self.peripheralManager) + self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.queue]; + } else { + if (self.peripheralManager) { + [self.peripheralManager stopAdvertising]; + self.peripheralManager.delegate = nil; + self.peripheralManager = nil; + } + } + + +} + +-(void)setShouldDiscover:(BOOL)shouldDiscover { + if(_shouldDiscover == shouldDiscover) + return; + + _shouldDiscover = shouldDiscover; + + if(shouldDiscover) { + if (!self.centralManager) + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.queue]; + if (!self.timer) + [self startTimer]; + } else { + if (self.centralManager) { + [self.centralManager stopScan]; + self.centralManager.delegate = nil; + self.centralManager = nil; + } + if (self.timer) + [self stopTimer]; + } +} + +-(instancetype)initWithUUID:(CBUUID *)uuid username:(NSString *)username usersBlock:(void (^)(NSArray *, BOOL))usersBlock { + self = [self initWithUUID:uuid username:username startOption:DIStartAdvertisingAndDetecting usersBlock:usersBlock]; + return self; +} + - (void)startTimer { self.timer = [NSTimer scheduledTimerWithTimeInterval:self.updateInterval target:self diff --git a/DiscoveryExample.xcodeproj/project.pbxproj b/DiscoveryExample.xcodeproj/project.pbxproj index 7df3aa7..429d98f 100644 --- a/DiscoveryExample.xcodeproj/project.pbxproj +++ b/DiscoveryExample.xcodeproj/project.pbxproj @@ -90,6 +90,7 @@ 17B568771A87864B007E0C5F = { isa = PBXGroup; children = ( + 9DC3B4FC1A9E0FCF00B1603B /* Discovery */, 17B568821A87864B007E0C5F /* DiscoveryExample */, 17B5689C1A87864B007E0C5F /* DiscoveryExampleTests */, 17B568811A87864B007E0C5F /* Products */, @@ -113,7 +114,6 @@ 17C70CB21A878EA200723200 /* Utils */, 17B568AD1A87885A007E0C5F /* Controllers */, 17B568871A87864B007E0C5F /* AppDelegate.h */, - 9DC3B4FC1A9E0FCF00B1603B /* Discovery */, 17B568881A87864B007E0C5F /* AppDelegate.m */, 17B568901A87864B007E0C5F /* Images.xcassets */, 17B568921A87864B007E0C5F /* LaunchScreen.xib */, @@ -255,7 +255,7 @@ TargetAttributes = { 17B5687F1A87864B007E0C5F = { CreatedOnToolsVersion = 6.1; - DevelopmentTeam = NZPAC7Q696; + DevelopmentTeam = 66SW7BQ2SM; SystemCapabilities = { com.apple.BackgroundModes = { enabled = 1; @@ -264,6 +264,7 @@ }; 17B568981A87864B007E0C5F = { CreatedOnToolsVersion = 6.1; + DevelopmentTeam = 66SW7BQ2SM; TestTargetID = 17B5687F1A87864B007E0C5F; }; };