我的情况:
sigkill()
->应用程序进入后台杀死了我的应用程序。centralManager: willRestoreState:
。centralManager: didDisconnectPeripheral: error: is invoked with error code 6.
[_centralManager connectPeripheral:peripheral options:nil]
,然后出现以下错误:[CoreBluetooth] API滥用:取消未使用外围设备的连接,您忘了保留对其的引用吗?
这个错误是什么意思?
如消息所示,您需要将CBPeripheral实例存储在对其有很强引用的位置。
通常,您可以通过将指针存储在某个地方来强烈引用该对象。例如,您可能有一个BluetoothConnectionManager保留了已连接外围设备的列表:
@implementation BluetoothConnectionManager
- (instancetype)init
{
if(self = [super init])
{
_knownPeripherals = [NSMutableArray array];
dispatch_queue_t centralQueue = dispatch_queue_create("com.my.company.app", DISPATCH_QUEUE_SERIAL);
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:@{CBCentralManagerOptionShowPowerAlertKey : @YES}];
}
return self;
}
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral
{
[_knownPeripherals addObject:peripheral];
}
- (void)centralManager:(CBCentralManager *)central
didDisconnectPeripheral:(CBPeripheral *)cbPeripheral
error:(NSError *)error
{
// This probably shouldn't happen, as you'll get the 'didConnectPeripheral' callback
// on any connected peripherals and add it there.
if(![_knownPeripherals containsObject:cbPeripheral])
{
[_knownPeripherals addObject:cbPeripheral];
}
[_centralManager connectPeripheral:cbPeripheral options:nil];
}
@end
或者,您可以修改此代码以引用单个连接的外围设备。
您还可以使用它来写出您以前的连接ID,以便在重新启动应用程序时尝试建立它们,如Apple Docs中所述
最后,关于参考的一些链接:
在“强引用和弱引用ios”上进行搜索将产生其他结果。如果您使用的是ARC,则只需拥有一个属性即可创建强大的参考。无论如何,将CBPeripheral实例添加到数组也将创建强引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句