Commit 836861fa authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- auto schedule start browsing/advertising later if device is not ready.

parent 00482f73
......@@ -19,6 +19,8 @@
{
NSMutableArray<NCMCMessageData*> *recMsgArray;
NSMutableArray<NCMCMessageData*> *dataToSend;
Boolean isBrowsingOrAdvertising;
}
@property (nonatomic, strong) dispatch_queue_t concurrentBluetoothLEDelegateQueue;
......@@ -91,6 +93,8 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
self.concurrentBluetoothLEDelegateQueue = nil;
self.serialDataSendingQueue = nil;
isBrowsingOrAdvertising = NO;
}
-(void)setupDispatchQueue
......@@ -266,6 +270,7 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
self.centralService = service;
self.isCentral = YES;
self.isDeviceReady = NO;
isBrowsingOrAdvertising = NO;
[self.session setSelfAsCentral];
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.concurrentBluetoothLEDelegateQueue];
}
......@@ -275,8 +280,12 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
if (self.isDeviceReady) {
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:self.session.serviceID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @NO}] ;
NSLog(@"BluetoothLE central scanning started");
} else {
NSLog(@"Device is not ready, scheduled for start later");
}
isBrowsingOrAdvertising = YES;
return self.isDeviceReady;
}
......@@ -284,6 +293,7 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
{
//self.centralService = nil;
[self.centralManager stopScan];
isBrowsingOrAdvertising = NO;
}
-(void)invitePeer:(NCMCPeerID *)peerID
......@@ -385,11 +395,17 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
[self.centralService notifyDidNotStartBrowsingForPeers:error];
}
self.isDeviceReady = NO;
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
self.isDeviceReady = YES;
if (isBrowsingOrAdvertising) {
[self startBrowsing];
}
}
}
......@@ -629,6 +645,7 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
self.peripheralService = service;
self.isCentral = NO;
self.isDeviceReady = NO;
isBrowsingOrAdvertising = NO;
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:self.concurrentBluetoothLEDelegateQueue];
}
......@@ -637,8 +654,12 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
if (self.isDeviceReady && self.session != nil) {
[self.peripheralManager startAdvertising:@{CBAdvertisementDataLocalNameKey:self.session.myPeerID.displayName,CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:self.session.serviceID]]}];
NSLog(@"peripheralManager startAdvertising...");
} else {
NSLog(@"Device is not ready, scheduled for start later");
}
isBrowsingOrAdvertising = YES;
return self.isDeviceReady;
}
......@@ -646,6 +667,7 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
{
//self.peripheralService = nil;
[self.peripheralManager stopAdvertising];
isBrowsingOrAdvertising = NO;
}
-(void)sendPeriheralData : (NSData*)data toCentral:(NSString*)identifier
......@@ -717,6 +739,9 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
[self.peripheralService notifyDidNotStartAdvertising:error];
}
self.isDeviceReady = NO;
return;
}
......@@ -740,6 +765,10 @@ static NCMCBluetoothLEManager *_sharedNCMCBluetoothLEManager = nil;
}
self.isDeviceReady = YES;
if (isBrowsingOrAdvertising) {
[self startAdvertising];
}
}
}
......
......@@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithSession:(NCMCSession*)ncmcsession NS_DESIGNATED_INITIALIZER;
- (Boolean)startBrowsingForPeers; // before call this function, should check isDeviceReady, otherwise this function would return NO
- (Boolean)startBrowsingForPeers;
- (void)stopBrowsingForPeers;
- (void)invitePeer:(NCMCPeerID *)peerID;
- (Boolean)isDeviceReady;
......
......@@ -18,8 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithSession:(NCMCSession*)session NS_DESIGNATED_INITIALIZER;
- (Boolean)startAdvertisingPeer;// before call this function, should check isDeviceReady, otherwise this function would return NO
- (Boolean)startAdvertisingPeer;
- (void)stopAdvertisingPeer;
- (Boolean)isDeviceReady;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment