如何在ios swift中连接低功耗蓝牙?

Sus*_*djo 5 ios bluetooth-lowenergy swift beacon

我想连接 ble 外设。但我的代码没有调用 didConect 函数

这是我的代码:

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
 let device = (advertisementData as NSDictionary)
            .object(forKey: CBAdvertisementDataLocalNameKey)
            as? NSString

        if device?.contains(BEAN_NAME) == true {

            print("Did discover peripheral", peripheral)

            self.bluetoothManager.stopScan()
            self._peripheral = peripheral
            self._peripheral.delegate = self
            central.connect(peripheral, options: nil)
        }
}


func centralManager( central: CBCentralManager, didConnect peripheral: CBPeripheral) { //cant call this
        print("connected to \(BEAN_NAME)")
        peripheral.discoverServices(nil)
    }
Run Code Online (Sandbox Code Playgroud)

日志:

BLE service is powered on
Did discover peripheral <CBPeripheral: 0x1740eef00, identifier = 4872623B-F872-443A-8A96-F4E1F84D6841, name = GoDoor in  :), state = disconnected>
Run Code Online (Sandbox Code Playgroud)

Sus*_*djo 2

我已经解决了这个问题。
只需要改变:

func centralManager(central: CBCentralManager, didConnect peripheral: CBPeripheral)
Run Code Online (Sandbox Code Playgroud)

进入这个:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
Run Code Online (Sandbox Code Playgroud)

参考:Swift 3:无法通过 BLE 连接到外设