简体   繁体   中英

core bluetooth crash in iOS 7

I have developed a bluetooth app, which runs fine on iOS 6, but when I run it on iOS 7 the application crashes in the -didDiscoverPeripheral call back. The crash info suggests that a release is called on the CBPeripheral object. I have used ARC for memory management, here is my declaration,initialisation of the CBPeripheral object and the call back code:

@interface BrLEDiscovery () <CBCentralManagerDelegate, CBPeripheralDelegate> {
    CBCentralManager    *centralManager;
    CBPeripheral *currentperipheral;
    BOOL                pendingInit;
}


- (id) init
{
    self = [super init];
    if (self) {
        pendingInit = YES;
        centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
       currentperipheral=[[CBPeripheral alloc]init];
        founddevice=FALSE;
        foundPeripherals = [[NSMutableArray alloc] init];
        connectedServices = [[NSMutableArray alloc] init];

    }
    return self;
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{ NSLog(@"Found Peripheral %@",[peripheral name]);

    if ([[peripheral name] isEqualToString:peripheralname]) {
        founddevice=TRUE;
        currentperipheral=peripheral;

        if (![foundPeripherals containsObject:peripheral]) {
            [foundPeripherals addObject:peripheral];
            [discoveryDelegate discoveryDidRefresh];
            [discoveryDelegate DiscoveryDidFindDevice:peripheral];

        }

        [RecursiveScanTimer invalidate];
    }


}

From your description, it's pretty likely to be crashing on this line:

currentperipheral=peripheral;

To me, it's kinda messy allocating a CBPeripheral object in your init and then assigning the peripheral at an unknown time later (especially with arc). If you want to keep reference to a discovered peripheral, just create a CBPeripheral object in your interface, retain the discovered peripheral, and assign it to your interface peripheral.

Just try something like this:

currentPeripheral = [discoveredPeripheral retain];//where discoveredPeripheral is the one given in the delegate callback

I personally don't use arc for any of my corebluetooth related applications..You need to be really careful with the corebluetooth framework and peripheral references though...it gets messyyy.

Note: If you do not always need to have direct contact with the peripheral, it's often handy to just keep a record of the CFUUIDRef (or identifier for iOS 7) and just retrieve the peripheral whenever you need it... Good luck!

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