mardi 17 mars 2015

CBCentralManager and Automatic Reference Counting


Vote count:

0




I have two applications which are very similar in their BLE functionality however one scans correctly and discovers peripherals where the other does not. They both report no problems initializing their pointer to CBCentralManager and checking that it's on and functional, but one goes on to discover and be able to connect to a device and 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 and ARC whereas the other uses a mix of ObjC and 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!



asked 55 secs ago







CBCentralManager and Automatic Reference Counting

Aucun commentaire:

Enregistrer un commentaire