CBP外设连接问题

问题描述 投票:0回答:1

CBCentralManager 在连接到设备时卡住。

开始扫描后,找到设备并到达centralManager:didDiscover:方法,然后尝试连接,但设备会保持连接状态一段时间,然后断开连接。

通过重新启动设备上的蓝牙可以轻松修复此问题,但这不是解决方案。有谁知道为什么会发生这种情况?

代码:

//MARK: CBCentralManagerDelegate methods

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        self.delegate?.centralServiceDidUpdateState(self)
        guard let centralManager = self.centralManager else { return }

        switch centralManager.state {
            case .poweredOn: self.scan()
            case .poweredOff: print("poweredOff")
            case .resetting: print("resetting")
            case .unauthorized: print("unauthorized")
            case .unknown: print("unknown")
            case .unsupported: print("unsupported")
        }
    }

    func scan() {
        self.centralManager?.scanForPeripherals(withServices: nil)
        print("Scanning started")
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

        self.delegate?.centralService(self, discover: peripheral.identifier.uuidString)
        let retrievedPeripherals = central.retrievePeripherals(withIdentifiers: [])

        for retrievedPeripheral in retrievedPeripherals {
            central.connect(retrievedPeripheral, options: nil)
        }

        if !retrievedPeripherals.contains(peripheral) {
            // And connect

            self.discoveredPeripheral = peripheral
            self.discoveredPeripheral?.delegate = self
            central.stopScan()

            central.connect(peripheral, options: nil)

            self.rssis[peripheral.identifier.uuidString] = "\(RSSI)"
            print("Connecting to peripheral \(peripheral)")
        }
    }

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print("Failed to connect to \(peripheral). (\(error!.localizedDescription))")
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Peripheral Connected with identifier: \(peripheral.identifier.uuidString)")
        self.discoveredPeripheral?.discoverServices(nil)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Discover services for peripheralIdentifier: \(peripheral.identifier.uuidString)")

        if let error = error {
            print("Error discovering services: \(error.localizedDescription)")
            return
        }
        // Loop through the newly filled peripheral.services array, just in case there's more than one.
        for service in peripheral.services ?? [] {
            peripheral.discoverCharacteristics(nil, for: service)
        }
    }

我还有来自控制台的日志:

2017-05-21 21:09:32.049137 MyApp[5871:2645747] [Fabric] Unable to locate application icon
2017-05-21 21:09:32.051153 MyApp[5871:2645747] [Crashlytics] Version 3.8.4 (121)
Scanning started
self.peripheralManager powered on.
Connecting to peripheral <CBPeripheral: 0x1700f9100, identifier = B5E6ACBF-307C-4C13-BB6A-09F4A92ABEC3, name = (null), state = connecting>

问题是不叫

centralManager: didFailToConnect
,不叫
centralManager: didConnect

也许这个问题与

null
外设名称有关?

更新#1:

我已经添加了

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 10.0) {
   print("Connecting to peripheral \(peripheral)")
}

我在控制台中得到了这个:

Connecting to peripheral <CBPeripheral: 0x1700f9100, identifier = B5E6ACBF-307C-4C13-BB6A-09F4A92ABEC3, name = (null), state = connecting>

所以10秒后外围仍然存在。所以这不是解除分配和强引用的问题。

ios swift bluetooth bluetooth-lowenergy
1个回答
0
投票

我也有类似的问题。 CBP外围设备卡在连接状态。

我的问题是我有 2 个不同的 CBCentralManager。

第一个中央管理器刚刚发现 CBPeripheral。

第二份担任后台中央管理员。

因此,当第一个经理发现外设时,我存储了这个 CBPeripheral。接下来,我尝试将存储的外围设备与第二个中央管理器连接。并且连接卡在连接状态。

我通过删除第二个中央管理器并仅使用第一个来解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.