Swift 3: Can't connect to peripheral via BLE

2019-04-28 21:46发布

问题:

I'm new to working with BLE, currently trying to make a simple application which would connect to my custom BLE device. I am able to discover the BLE device, but for some reason i can't connect to it. I tried to check it with 'Light Blue', it shows my device as connectable and seems to work fine. But in my app after i discover the device, CB manager tries to connect to it and seems to 'freeze'? Function 'didConnect peripheral' is never triggered, and state of peripheral is forever 'connecting'.

How can i identify the problem? Is there any options i can include in connection method, or somehow track the connection process?

I would appreciate any advice where to search for problems.

Working in XCode 8.2.1, using Swift 3. iOS 10.2.1 installed on the testing phone

Here's my code:

import UIKit
import CoreBluetooth

class InfoPageViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

var manager:CBCentralManager!
var peripheral:CBPeripheral!

let BEAN_NAME = "MyDevice"

override func viewDidLoad() {
    super.viewDidLoad()

    manager = CBCentralManager(delegate: self, queue: nil)
}

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 {
        self.manager.stopScan()

        self.peripheral = peripheral
        self.peripheral.delegate = self

        manager.connect(peripheral, options: nil)

        print("discovered \(BEAN_NAME)")

    }
}

func centralManager(
    central: CBCentralManager,
    didConnect peripheral: CBPeripheral) {
    print("connected to \(BEAN_NAME)")

    peripheral.discoverServices(nil)
}

回答1:

func centralManager(central: CBCentralManager, didConnect peripheral: CBPeripheral) {}
--------------------^

Versus:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
--------------------^

The signature of the method is not the correct one, you are missing the _.

Method signatures are important. We can assume, since theses delegate methods are optional, that internally, the Apple code asks itself: Does my delegate have the method func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) implemented (respondsToSelector:)? In your case, not, because it's not the same, and then yours is not called.

You copy/paste the one from the doc or remove it and let XCode do its autocompletion thing.