0

I have the following very simple code snippet on El Capitan which is basically scanning BLE peripherals and connecting to the one I want. However, didDiscoverPeripheral is never called! It starts scanning but never finds any peripheral. A few answers to future clarification requests:

  1. I have BLE devices advertising in the vicinity. I can see them from the phone for example.

  2. The code checks if the BLE is poweredOn.

  3. There are many similar questions such as below but none of them have the working answer for me.

Question 1

Question 2

Question 3

Question 4

The code is as follows:

import Cocoa
import CoreBluetooth

class ViewController: NSViewController, CBPeripheralDelegate, CBCentralManagerDelegate {

var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
let DEVICE_NAME = "something"
var currentCharacteristic: CBCharacteristic! = nil

// Put CentralManager in the main queue
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.startManager()

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.startManager()
    // Do any additional setup after loading the view.
}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}

func startManager(){
    centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    switch (central.state)
    {
    case CBCentralManagerState.unsupported:
        print("BLE Unsupported")
        break
    case CBCentralManagerState.unauthorized:
        print("BLE unauthorized")
        break
    case CBCentralManagerState.poweredOff:
        print("BLE is not on.")
        break
    case CBCentralManagerState.poweredOn:
        print("BLE is on.")
        print("Scanning...")
        self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
        break
    case CBCentralManagerState.unknown:
        print("BLE state unknown")
        break
    default:
        print("BLE state default.")
    }
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    // This is NEVER called!
    print("Peripheral: \(peripheral)")
    if (peripheral.name != nil && peripheral.name! == DEVICE_NAME){
        self.peripheral = peripheral
        self.centralManager.connect(self.peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
    }
}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    peripheral.delegate = self
    peripheral.discoverServices(nil)
    print("Connected to \(peripheral)")
    self.stopScan()
}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?){
    print("connection failed", error)
}

func stopScan(){
    self.centralManager.stopScan()
}


func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?){
    if self.peripheral != nil {
        self.peripheral.delegate = nil
        self.peripheral = nil
    }
    print("Disconnected.", error)
    self.startManager()
}
}

Any idea what's wrong?

Community
  • 1
  • 1
mco
  • 422
  • 1
  • 4
  • 17
  • You are using Swift 3, your methods are the one of Swift 2.2. Look at the doc, their signature changed! For instance: `func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)`=> `func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)` Same issue as there: http://stackoverflow.com/questions/40491818/didselectrowatindexpath-not-working-swift-3/40492091#40492091 – Larme Dec 01 '16 at 09:15
  • Thanks a lot! Yes, that was the problem indeed. – mco Dec 01 '16 at 12:48

0 Answers0