繁体   English   中英

iOS 8.2 CoreBluetooth-CentralManager不调用委托didDiscoverPeripherals

[英]iOS 8.2 CoreBluetooth - CentralManager not calling delegate didDiscoverPeripherals

自从升级到iOS 8.2以来,我似乎在使用CoreBluetooth遇到了麻烦。 我以前有一个运行CBCentralManager子类的应用程序,可以扫描我想要的UUID。 现在,我无法获得对didDiscoverPeripherals的委托回调。

我已验证外围设备是否可以通过LightBlue正确广播。 我已验证在调用scan之前已打开CentralManager状态。 我尝试使用UUID和nil进行扫描。 同样,在升级之前,我让此代码工作了数周。 是否有其他人经历过此事,或者不知道会发生什么?

编辑-添加代码CBCentralManager子类代码

@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");
}

在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];
    }
}

根据用户是否已登录还是centralManager打开startScan来调用startScan两次。 我基本上将peripheralManager视为相同,并且可以正常工作(由浅蓝色验证)。

看起来问题与子类化CBCentralManager 也许是由于iOS 8.2中的这些最新更改 ,因为它之前运行良好。 一个快速的解决方案是在子类中添加一个ivar manager并初始化CBCentralManager 然后在该经理上致电scan 尽管这可能不是一个干净的方法,但它突出了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM