<错误>:[CoreBluetooth] API错误:取消未使用外设的连接

Nachum

我的情况:

  1. 我用sigkill()->应用程序进入后台杀死了我的应用程序。
  2. 数据是从BT设备发送的,并且在按需调用时成功连接centralManager: willRestoreState:
  3. 连接设备后,将BT设备移出应用程序范围和方法 centralManager: didDisconnectPeripheral: error: is invoked with error code 6.
  4. 我尝试通过调用重新连接外围设备[_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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CoreBluetooth 外设:didDiscoverServices 未触发

来自分类Dev

是否可以以编程方式访问CoreBluetooth记录的错误代码?

来自分类Dev

iPad Air-CoreBluetooth [错误]取消选择器,因为我们无法与蓝牙通信

来自分类Dev

连接后立即断开Corebluetooth

来自分类Dev

CoreBluetooth外设.setNotifyValue(true,特征)正在通知

来自分类Dev

iOS CoreBluetooth Per / Centr交叉连接

来自分类Dev

Swift中未调用CoreBluetooth didDiscoverPeripheral

来自分类Dev

使用CoreBluetooth提高文件传输速度

来自分类Dev

使用Swift和CoreBluetooth创建静态特征

来自分类Dev

使用CoreBluetooth提高文件传输速度

来自分类Dev

错误:由于未连接表单而取消了表单提交

来自分类Dev

如何使用CoreBluetooth在中央附近找到所有已知的BLE外设?

来自分类Dev

连接未关闭错误

来自分类Dev

CoreBluetooth`retrieveConnectedPeripheralsWithServices`返回断开连接的外围设备

来自分类Dev

CoreBluetooth 外围服务在重新连接/恢复时变空

来自分类Dev

Swift:[CoreBluetooth] XPC连接在取消弹出框控制器后无效

来自分类Dev

如何在IOBluetooth / CoreBluetooth中使用服务请求?

来自分类Dev

使用CoreBluetooth可以获取Bluetooth LE设备的原始扫描记录

来自分类Dev

您可以在Xcode模拟器上使用CoreBluetooth吗?

来自分类Dev

CoreBluetooth:scanForPeripheralsWithServices无法与服务数组一起使用

来自分类Dev

快速将UUID与CoreBluetooth和HealthKit一起使用

来自分类Dev

您可以在Xcode模拟器上使用CoreBluetooth吗?

来自分类Dev

应用程序在后台使用CoreBluetooth进行通信

来自分类Dev

CoreBluetooth:scanForPeripheralsWithServices无法与服务数组一起使用

来自分类Dev

使用PHP的Twitch API连接错误

来自分类Dev

使用301错误错误连接到gtalk

来自分类Dev

'未登录'错误Facebook API

来自分类Dev

CQLSH连接错误:错误的请求:未配置的columnfamily本地

来自分类Dev

Demuxer:未连接的电线/ firrtl错误

Related 相关文章

  1. 1

    CoreBluetooth 外设:didDiscoverServices 未触发

  2. 2

    是否可以以编程方式访问CoreBluetooth记录的错误代码?

  3. 3

    iPad Air-CoreBluetooth [错误]取消选择器,因为我们无法与蓝牙通信

  4. 4

    连接后立即断开Corebluetooth

  5. 5

    CoreBluetooth外设.setNotifyValue(true,特征)正在通知

  6. 6

    iOS CoreBluetooth Per / Centr交叉连接

  7. 7

    Swift中未调用CoreBluetooth didDiscoverPeripheral

  8. 8

    使用CoreBluetooth提高文件传输速度

  9. 9

    使用Swift和CoreBluetooth创建静态特征

  10. 10

    使用CoreBluetooth提高文件传输速度

  11. 11

    错误:由于未连接表单而取消了表单提交

  12. 12

    如何使用CoreBluetooth在中央附近找到所有已知的BLE外设?

  13. 13

    连接未关闭错误

  14. 14

    CoreBluetooth`retrieveConnectedPeripheralsWithServices`返回断开连接的外围设备

  15. 15

    CoreBluetooth 外围服务在重新连接/恢复时变空

  16. 16

    Swift:[CoreBluetooth] XPC连接在取消弹出框控制器后无效

  17. 17

    如何在IOBluetooth / CoreBluetooth中使用服务请求?

  18. 18

    使用CoreBluetooth可以获取Bluetooth LE设备的原始扫描记录

  19. 19

    您可以在Xcode模拟器上使用CoreBluetooth吗?

  20. 20

    CoreBluetooth:scanForPeripheralsWithServices无法与服务数组一起使用

  21. 21

    快速将UUID与CoreBluetooth和HealthKit一起使用

  22. 22

    您可以在Xcode模拟器上使用CoreBluetooth吗?

  23. 23

    应用程序在后台使用CoreBluetooth进行通信

  24. 24

    CoreBluetooth:scanForPeripheralsWithServices无法与服务数组一起使用

  25. 25

    使用PHP的Twitch API连接错误

  26. 26

    使用301错误错误连接到gtalk

  27. 27

    '未登录'错误Facebook API

  28. 28

    CQLSH连接错误:错误的请求:未配置的columnfamily本地

  29. 29

    Demuxer:未连接的电线/ firrtl错误

热门标签

归档