简体   繁体   中英

CBCentralManager and Automatic Reference Counting

I have two applications which are very similar in their BLE functionality however one scans correctly & discovers peripherals where other does not.

They both report no problems initializing their pointer to CBCentralManager & checking that it's on & functional, but one goes on to discover & be able to connect to a device & the other does not.

In the one that doesn't work the call to scanForPeripheralsWithServices goes off without a hitch but the didDiscoverPeripheral callback is never triggered. The biggest difference that i can think of is that one project uses pure Objective-C & ARC whereas the other uses a mix of Objective-C & C++ and does not use automatic reference counting.

Could this be the source of the problem ? If so, is there a way around it? I have been able to work with CoreBluetooth in the past without ARC but it might have changed without my being able to parse that from the docs.

In the event that it's not ARC, the two calls to scanForPeripheralsWithServices are shown below:

functional

- (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 !
}

non-functional

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

And the relevant parts of the two header files:

functional

@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;

non-functional

@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{

}

+ (BLEDeviceManager *)sharedDeviceManager;

@property (strong, nonatomic) CBCentralManager *central;

Any advice much appreciated!

So this is a bit silly but a reboot of the phone + clean/build fixed everything. I suspect that the two applications weren't playing nice with one another in releasing the CBCentralManager handle but that's just a guess. I'm not deeply familiar with CoreBluetooth or with Objective C to be honest so my guesses aren't all that well informed.

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.

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