gpt4 book ai didi

ios - iOS 7 中的核心蓝牙崩溃

转载 作者:行者123 更新时间:2023-11-29 10:54:36 25 4
gpt4 key购买 nike

我开发了一个蓝牙应用程序,它在 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];
}


}

最佳答案

根据您的描述,它很可能在这条线上崩溃:

currentperipheral=peripheral;

对我来说,在您的 init 中分配一个 CBPeripheral 对象,然后在以后未知的时间分配外围设备(尤其是使用 arc)有点麻烦。如果您想保留对已发现外围设备的引用,只需在您的接口(interface)中创建一个 CBPeripheral 对象,保留已发现的外围设备,并将其分配给您的接口(interface)外围设备。

试试这样的:

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

我个人不会在我的任何 corebluetooth 相关应用程序中使用 arc。但是你需要非常小心 corebluetooth 框架和外围引用......它变得困惑。

注意:如果您并不总是需要与外围设备直接接触,通常只记录 CFUUIDRef(或 identifier for iOS 7)通常很方便) 并在需要时取回外围设备...祝你好运!

关于ios - iOS 7 中的核心蓝牙崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com