我开发了一个蓝牙应用程序,它在 iOS 6 上运行良好,但是当我在 iOS 7 上运行它时,应用程序在 -didDiscoverPeripheral 回调中崩溃。崩溃信息表明在 CBPeripheral 对象上调用了一个版本。我使用 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];
}
}