简体   繁体   English

iOS 7中的核心蓝牙崩溃

[英]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. 我已经开发了一个蓝牙应用程序,可以在iOS 6上正常运行,但是当我在iOS 7上运行它时,该应用程序在-didDiscoverPeripheral回调中崩溃。 The crash info suggests that a release is called on the CBPeripheral object. 崩溃信息表明在CBPeripheral对象上调用了发行版。 I have used ARC for memory management, here is my declaration,initialisation of the CBPeripheral object and the call back code: 我已使用ARC进行内存管理,这是我的声明,CBPeripheral对象的初始化和回调代码:

@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). 对我来说,在您的init中分配一个CBPeripheral对象,然后在一个未知的时间(特别是使用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. 如果要保留对发现的外围设备的引用,只需在界面中创建CBPeripheral对象,保留发现的外围设备,然后将其分配给接口外围设备。

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. 我个人不将arc用于我的任何与corebluetooth相关的应用程序。.您需要非常小心corebluetooth框架和外围设备引用,尽管它会变得杂乱无章。

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! 注意:如果您并不总是需要直接与外设直接接触,那么记录CFUUIDRef (或iOS 7的identifier )并在需要时就检索外设通常很方便……祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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