Skip to content

Commit

Permalink
Merge pull request #13 from joshblour/startOptions
Browse files Browse the repository at this point in the history
Start options
  • Loading branch information
omergul committed Mar 21, 2015
2 parents 173f3b5 + 8230267 commit 3be333b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
28 changes: 27 additions & 1 deletion Discovery/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@
#import <CoreLocation/CoreLocation.h>
#import "BLEUser.h"

/** Start options */
typedef NS_ENUM(NSInteger, DIStartOptions) {
DIStartAdvertisingAndDetecting = 0,
DIStartAdvertisingOnly,
DIStartDetectingOnly,
DIStartNone
};

@interface Discovery : NSObject<CBCentralManagerDelegate, CBPeripheralManagerDelegate, CBPeripheralDelegate>


/**
* 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
Expand All @@ -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.
Expand Down
74 changes: 70 additions & 4 deletions Discovery/Discovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,6 +30,8 @@ - (instancetype)initWithUUID:(CBUUID *)uuid
_userTimeoutInterval = 3;
_updateInterval = 2;



// listen for UIApplicationDidEnterBackgroundNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidEnterBackground:)
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions DiscoveryExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
17B568771A87864B007E0C5F = {
isa = PBXGroup;
children = (
9DC3B4FC1A9E0FCF00B1603B /* Discovery */,
17B568821A87864B007E0C5F /* DiscoveryExample */,
17B5689C1A87864B007E0C5F /* DiscoveryExampleTests */,
17B568811A87864B007E0C5F /* Products */,
Expand All @@ -113,7 +114,6 @@
17C70CB21A878EA200723200 /* Utils */,
17B568AD1A87885A007E0C5F /* Controllers */,
17B568871A87864B007E0C5F /* AppDelegate.h */,
9DC3B4FC1A9E0FCF00B1603B /* Discovery */,
17B568881A87864B007E0C5F /* AppDelegate.m */,
17B568901A87864B007E0C5F /* Images.xcassets */,
17B568921A87864B007E0C5F /* LaunchScreen.xib */,
Expand Down Expand Up @@ -255,7 +255,7 @@
TargetAttributes = {
17B5687F1A87864B007E0C5F = {
CreatedOnToolsVersion = 6.1;
DevelopmentTeam = NZPAC7Q696;
DevelopmentTeam = 66SW7BQ2SM;
SystemCapabilities = {
com.apple.BackgroundModes = {
enabled = 1;
Expand All @@ -264,6 +264,7 @@
};
17B568981A87864B007E0C5F = {
CreatedOnToolsVersion = 6.1;
DevelopmentTeam = 66SW7BQ2SM;
TestTargetID = 17B5687F1A87864B007E0C5F;
};
};
Expand Down

0 comments on commit 3be333b

Please sign in to comment.