CBPeripheral连接问题

时间:2017-05-21 20:05:33

标签: ios swift bluetooth bluetooth-lowenergy

连接到设备时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秒后外围仍然存在。所以这不是解除分配和强引用的问题。

0 个答案:

没有答案