From 17307b86cefe26f5e35f36ea2dc5d797b98d0155 Mon Sep 17 00:00:00 2001 From: Yonah Date: Wed, 25 Feb 2015 15:54:07 +0100 Subject: [PATCH 1/4] added options to start with advertising, detecting, both, or none --- Discovery/Discovery.h | 22 ++++++- Discovery/Discovery.m | 74 ++++++++++++++++++++-- DiscoveryExample.xcodeproj/project.pbxproj | 3 +- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/Discovery/Discovery.h b/Discovery/Discovery.h index 724f5f5..4f4eb88 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 diff --git a/Discovery/Discovery.m b/Discovery/Discovery.m index e1c1b17..e78665a 100644 --- a/Discovery/Discovery.m +++ b/Discovery/Discovery.m @@ -11,11 +11,15 @@ @interface Discovery() @property (nonatomic, copy) void (^usersBlock)(NSArray *users, BOOL usersChanged); @property (strong, nonatomic) NSTimer *timer; +@property (nonatomic) BOOL shouldAdvertise; +@property (nonatomic) BOOL shouldDetect; @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 +32,8 @@ - (instancetype)initWithUUID:(CBUUID *)uuid _userTimeoutInterval = 3; _updateInterval = 2; + + // listen for UIApplicationDidEnterBackgroundNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) @@ -47,15 +53,75 @@ - (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; + _shouldDetect = NO; + + switch (startOption) { + case DIStartAdvertisingAndDetecting: + self.shouldAdvertise = YES; + self.shouldDetect = YES; + break; + case DIStartAdvertisingOnly: + self.shouldAdvertise = YES; + break; + case DIStartDetectingOnly: + self.shouldDetect = YES; + break; + case DIStartNone: + default: + break; + } - [self startTimer]; } return self; } +-(void)setShouldAdvertise:(BOOL)shouldAdvertise { + if(_shouldAdvertise == shouldAdvertise) + return; + + _shouldAdvertise = shouldAdvertise; + + if(shouldAdvertise) { + 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]; + } +} + +-(void)setShouldDetect:(BOOL)shouldDetect { + if(_shouldDetect == shouldDetect) + return; + + _shouldDetect = shouldDetect; + + if(shouldDetect) { + 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; + } + } +} + +-(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..6ab8a1a 100644 --- a/DiscoveryExample.xcodeproj/project.pbxproj +++ b/DiscoveryExample.xcodeproj/project.pbxproj @@ -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; }; }; From d7ebf3d52a227a57f1d90865f3d1d592cdab3c4c Mon Sep 17 00:00:00 2001 From: Yonah Date: Wed, 25 Feb 2015 16:37:10 +0100 Subject: [PATCH 2/4] renamed detect to discover. made bool properties public --- Discovery/Discovery.h | 6 ++++++ Discovery/Discovery.m | 14 ++++++-------- DiscoveryExample.xcodeproj/project.pbxproj | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Discovery/Discovery.h b/Discovery/Discovery.h index 4f4eb88..6ae8745 100644 --- a/Discovery/Discovery.h +++ b/Discovery/Discovery.h @@ -48,6 +48,12 @@ typedef NS_ENUM(NSInteger, DIStartOptions) { */ - (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 e78665a..8faea01 100644 --- a/Discovery/Discovery.m +++ b/Discovery/Discovery.m @@ -11,8 +11,6 @@ @interface Discovery() @property (nonatomic, copy) void (^usersBlock)(NSArray *users, BOOL usersChanged); @property (strong, nonatomic) NSTimer *timer; -@property (nonatomic) BOOL shouldAdvertise; -@property (nonatomic) BOOL shouldDetect; @end @implementation Discovery @@ -54,18 +52,18 @@ - (instancetype)initWithUUID:(CBUUID *)uuid self.queue = dispatch_queue_create("com.omerfarukgul.discovery", DISPATCH_QUEUE_SERIAL); _shouldAdvertise = NO; - _shouldDetect = NO; + _shouldDiscover = NO; switch (startOption) { case DIStartAdvertisingAndDetecting: self.shouldAdvertise = YES; - self.shouldDetect = YES; + self.shouldDiscover = YES; break; case DIStartAdvertisingOnly: self.shouldAdvertise = YES; break; case DIStartDetectingOnly: - self.shouldDetect = YES; + self.shouldDiscover = YES; break; case DIStartNone: default: @@ -99,11 +97,11 @@ -(void)setShouldAdvertise:(BOOL)shouldAdvertise { } } --(void)setShouldDetect:(BOOL)shouldDetect { - if(_shouldDetect == shouldDetect) +-(void)setShouldDiscover:(BOOL)shouldDetect { + if(_shouldDiscover == shouldDetect) return; - _shouldDetect = shouldDetect; + _shouldDiscover = shouldDetect; if(shouldDetect) { if (!self.peripheralManager) diff --git a/DiscoveryExample.xcodeproj/project.pbxproj b/DiscoveryExample.xcodeproj/project.pbxproj index 6ab8a1a..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 */, From 672f3731fa1f1ce71b5758d362952d60deb52eb4 Mon Sep 17 00:00:00 2001 From: Yonah Date: Wed, 25 Feb 2015 16:38:53 +0100 Subject: [PATCH 3/4] forgot one place to rename detect to discover --- Discovery/Discovery.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Discovery/Discovery.m b/Discovery/Discovery.m index 8faea01..a348e5d 100644 --- a/Discovery/Discovery.m +++ b/Discovery/Discovery.m @@ -97,13 +97,13 @@ -(void)setShouldAdvertise:(BOOL)shouldAdvertise { } } --(void)setShouldDiscover:(BOOL)shouldDetect { - if(_shouldDiscover == shouldDetect) +-(void)setShouldDiscover:(BOOL)shouldDiscover { + if(_shouldDiscover == shouldDiscover) return; - _shouldDiscover = shouldDetect; + _shouldDiscover = shouldDiscover; - if(shouldDetect) { + if(shouldDiscover) { if (!self.peripheralManager) self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.queue]; } else { From 823026771e3ea1626222db85ac9a3b70f7518b38 Mon Sep 17 00:00:00 2001 From: Yonah Date: Wed, 25 Feb 2015 18:25:45 +0100 Subject: [PATCH 4/4] mixed up the implementation of advertise and discover. fixed --- Discovery/Discovery.m | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Discovery/Discovery.m b/Discovery/Discovery.m index a348e5d..3a644e0 100644 --- a/Discovery/Discovery.m +++ b/Discovery/Discovery.m @@ -82,19 +82,17 @@ -(void)setShouldAdvertise:(BOOL)shouldAdvertise { _shouldAdvertise = shouldAdvertise; if(shouldAdvertise) { - if (!self.centralManager) - self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.queue]; - if (!self.timer) - [self startTimer]; + if (!self.peripheralManager) + self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.queue]; } else { - if (self.centralManager) { - [self.centralManager stopScan]; - self.centralManager.delegate = nil; - self.centralManager = nil; + if (self.peripheralManager) { + [self.peripheralManager stopAdvertising]; + self.peripheralManager.delegate = nil; + self.peripheralManager = nil; } - if (self.timer) - [self stopTimer]; } + + } -(void)setShouldDiscover:(BOOL)shouldDiscover { @@ -104,14 +102,18 @@ -(void)setShouldDiscover:(BOOL)shouldDiscover { _shouldDiscover = shouldDiscover; if(shouldDiscover) { - if (!self.peripheralManager) - self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.queue]; + if (!self.centralManager) + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.queue]; + if (!self.timer) + [self startTimer]; } else { - if (self.peripheralManager) { - [self.peripheralManager stopAdvertising]; - self.peripheralManager.delegate = nil; - self.peripheralManager = nil; + if (self.centralManager) { + [self.centralManager stopScan]; + self.centralManager.delegate = nil; + self.centralManager = nil; } + if (self.timer) + [self stopTimer]; } }