Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreBluetooth didDiscoverPeripheral not being called in Swift

I am 99% sure I followed the instructions to setup CoreBluetooth correctly. No matter what I do, when I run this app on my iPad mini, the Bluetooth is saying its on. It's saying it is scanning for devices, but it is absolutely not finding any devices. If I go to the Bluetooth menu on the device I do see other devices being discovered. I initialize the CBCentralManager. I setup centralManagerDidUpdateState. When that is sure the bluetooth is ready it calls centralManager.scanForPeripheralsWithServices. All this is happening correctly. But my delegate function centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) never ever gets called. My code is very simple. Maybe I am missing something but I was able to confirm that my Macbook is a BTLE device and my ipad mini is a BTLE device as well. Here is my code.

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

    var centralManager:CBCentralManager!
    var blueToothReady = false

    override func viewDidLoad() {
        super.viewDidLoad()
        startUpCentralManager()
    }

    func startUpCentralManager() {
        println("Initializing central manager")
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func discoverDevices() {
        println("discovering devices")
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        println("Discovered \(peripheral.name)")
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("checking state")
        switch (central.state) {
            case .PoweredOff:
            println("CoreBluetooth BLE hardware is powered off")

            case .PoweredOn:
            println("CoreBluetooth BLE hardware is powered on and ready")
            blueToothReady = true;

            case .Resetting:
            println("CoreBluetooth BLE hardware is resetting")

            case .Unauthorized:
            println("CoreBluetooth BLE state is unauthorized")

            case .Unknown:
            println("CoreBluetooth BLE state is unknown");

            case .Unsupported:
            println("CoreBluetooth BLE hardware is unsupported on this platform");

        }
        if blueToothReady {
            discoverDevices()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
like image 849
Johnston Avatar asked Jul 30 '14 14:07

Johnston


1 Answers

I had to get my macbook advertising. Once I did that using https://github.com/mttrb/BeaconOSX it worked exactly as I had written it.

like image 130
Johnston Avatar answered Sep 23 '22 06:09

Johnston