CBCentralManager和自动引用计数

时间:2015-03-18 05:31:59

标签: c++ ios objective-c bluetooth core-bluetooth

我有两个应用程序,它们的BLE功能非常相似,但是一个正确扫描和发现其他没有的外围设备。

他们都报告初始化指针CBCentralManager&检查它是否已开启&功能,但一个继续发现&能够连接到设备&另一个没有。

在无法正常工作的情况下,对scanForPeripheralsWithServices的调用顺利进行,但永远不会触发didDiscoverPeripheral回调。我能想到的最大区别是一个项目使用纯Objective-C& ARC而另一个使用Objective-C&的混合C ++并且不使用自动引用计数。

这可能是问题的根源吗?如果是这样,有办法吗?在过去没有ARC的情况下,我能够与CoreBluetooth合作,但如果我无法从文档中解析出来,那么它可能已经发生了变化。

如果它不是ARC,则对scanForPeripheralsWithServices的两次调用如下所示:

功能

- (int) findBLEPeripherals:(int) timeout
{
    NSLog(@"start finding");

    if (self.CM.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth not correctly initialized !");
        NSLog(@"State = %d (%s)\r\n", self.CM.state, [self centralManagerStateToString:self.CM.state]);
        return -1;
    }

    [NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];


    [self.CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@RBL_SERVICE_UUID]] options:nil];

    NSLog(@"scanForPeripheralsWithServices");

    return 0; // Started scanning OK !
}

非功能

- (void)startScan
{
    NSLog(@"startScan");

    isScanning = true;

    NSDictionary *options = nil;

    didUpdateDiscoveredDeviceFlag = [delegate respondsToSelector:@selector(didUpdateDiscoveredRFduino:)];

    if (didUpdateDiscoveredDeviceFlag) {
        options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
        forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
    }

    [devices removeAllObjects];

    if (self.central.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth not correctly initialized !");
       // NSLog(@"State = %d (%s)\r\n", self.central.state, [self centralManagerStateToString:self.central.state]);
    }

    [NSTimer scheduledTimerWithTimeInterval:(float)60 target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];

    [self.central scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@RBL_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

    if (didUpdateDiscoveredDeviceFlag) {
        [self startRangeTimer];
    }
}

两个头文件的相关部分:

功能

@interface BLE : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate> {

}

@property (nonatomic,assign) id <BLEDelegate> delegate;
@property (strong, nonatomic) NSMutableArray *peripherals;
@property (strong, nonatomic) NSMutableArray *peripheralsRssi;
@property (strong, nonatomic) CBCentralManager *CM;
@property (strong, nonatomic) CBPeripheral *activePeripheral;

非功能

@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{

}

+ (BLEDeviceManager *)sharedDeviceManager;

@property (strong, nonatomic) CBCentralManager *central;

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:0)

所以这有点傻但是手机的重新启动+清理/构建修复了一切。我怀疑这两个应用程序在发布CBCentralManager句柄时并没有很好地互相玩,但这只是一个猜测。我对CoreBluetooth或Objective C并不十分熟悉,所以我的猜测并不是很清楚。

相关问题