Since upgrading to iOS 8.2 I seem to be having trouble with CoreBluetooth. I had a previous application working that ran a CBCentralManager subclass to scan for UUIDs I wanted. Now, I'm unable to get a delegate callback to didDiscoverPeripherals
.
I've verified that the peripherals are broadcasting correctly via LightBlue. I've verified that the CentralManager state is powered on before calling scan. I tried scanning both with my UUID and also with nil. Again, I had this code working for weeks before upgrading. Has anyone else experienced this or have any idea what could be going on?
EDIT - added code CBCentralManager subclass code
@implementation CentralManager
-(id) initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue {
self = [super initWithDelegate:delegate
queue:queue
options:nil];
_items = [NSMutableDictionary new];
_discoveredPeripherals = [NSMutableArray new];
_app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
-(void) startScan {
if (!_isScanning && self.state == CBCentralManagerStatePoweredOn && _app.loggedIn) {
[self scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:APP_SERVICE_UUID]] options:nil];
_isScanning = YES;
NSLog(@"Scanning started");
}
}
-(void) stopScan {
[super stopScan];
_isScanning = NO;
NSLog(@"Scanning stopped");
}
In AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (!_centralManager) {
_centralQueue = dispatch_queue_create("centralQueue", nil);
_centralManager = [[CentralManager alloc]initWithDelegate:self queue:_centralQueue];
}
[_peripheralManager beginAdvertising];
[_centralManager startScan];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
[_centralManager startScan];
}
}
startScan
is called twice depending on if user is logged in or centralManager
is powered on first. I basically treat peripheralManager
identically and that works (verified by Light Blue).
It looks like the issue has to do with subclassing CBCentralManager
. Maybe because of these recent changes in iOS 8.2, because it worked fine before. A quick fix is to add an ivar manager
in the subclass and initialize a CBCentralManager
. Then call scan
on that manager. While this is probably not a clean approach, it highlights the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.