Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: Can't connect to peripheral via BLE

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)
}
like image 739
Alice A. Avatar asked Feb 07 '17 13:02

Alice A.


1 Answers

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.

like image 113
Larme Avatar answered Oct 11 '22 13:10

Larme