Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 8.2 CoreBluetooth-CentralManager不调用delegate DidDiscovery外围设备_Ios_Core Bluetooth - Fatal编程技术网

iOS 8.2 CoreBluetooth-CentralManager不调用delegate DidDiscovery外围设备

iOS 8.2 CoreBluetooth-CentralManager不调用delegate DidDiscovery外围设备,ios,core-bluetooth,Ios,Core Bluetooth,自从升级到iOS 8.2后,我似乎在CoreBooth方面遇到了麻烦。我以前有一个应用程序运行CBCentralManager子类来扫描我想要的UUID。现在,我无法获得对didiscoverperipherals的代理回调 我已经通过LightBlue验证了外围设备是否正确广播。在调用scan之前,我已验证CentralManager状态已打开。我试着用UUID和nil扫描。同样,在升级之前,我让这段代码运行了数周。有没有其他人经历过这种情况,或者知道会发生什么 编辑-添加代码CBCentra

自从升级到iOS 8.2后,我似乎在CoreBooth方面遇到了麻烦。我以前有一个应用程序运行CBCentralManager子类来扫描我想要的UUID。现在,我无法获得对
didiscoverperipherals
的代理回调

我已经通过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");
}

startScan
会被调用两次,具体取决于用户是登录还是先打开了
centralManager
。我基本上是以相同的方式对待
外围设备管理器
,这是有效的(用浅蓝色验证)。

看起来问题与子类化
CBCentralManager
有关。也许是因为iOS 8.2中最近的这些功能,因为它以前工作得很好。快速修复方法是在子类中添加ivar
管理器
,并初始化
CBCentralManager
。然后在该管理器上调用
scan
。虽然这可能不是一个干净的方法,但它突出了问题。

您是否在属性中存储对
CBCentralManager
的引用?你能告诉我你在哪里安装中央设备和扫描外围设备吗?我在上面添加了一些相关的代码。是的,我将我的
CBCentralManager
子类ref存储在
AppDelegate
属性中。您是否尝试过再次关闭和打开设备?我曾经遇到过这样的问题:蓝牙无缘无故不合作,再次关闭和打开修复了它。我不认为这是你的问题,但对CBCentralManager进行子类化,让它保存对你的应用程序委托的引用,并将应用程序代码放入其中,这很讨厌。另外,请尝试养成使用
self.property
而不是
\u property
的习惯,除非您明确希望绕过setter/getterI。我看到过重新启动设备修复此类问题的情况。我甚至在工厂重置了我的一个测试设备。然而,这一次没有帮助。另外-我在整个应用程序文件(即子类、控制器)中使用了对app的弱引用,以方便AppDelegate。这会因为任何原因导致意外行为吗?@Paulw11如果你想发布一个答案来说明这里发生了什么(即为什么子类化
CBCentralManager
不能像我原来的方式工作),我可以将其标记为已接受的答案。
- (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];
    }
}