1

why isn't the CBPeripheralManagerDelegate method callinga although i have initialized the class as var bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)

and already added BlueTooth Framework in my Bundle.

import UIKit
import CoreBluetooth

class AppSettingsViewController: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()

       var bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)


    }

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


}

extension AppSettingsViewController : CBPeripheralManagerDelegate{


    func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {

        if peripheral.state == CBPeripheralManagerState.PoweredOn{

            println("broadcasting")



        }else if peripheral.state == CBPeripheralManagerState.PoweredOff{

            println("no bluetooth found")

        }else if peripheral.state == CBPeripheralManagerState.Unauthorized {

            println("This option is not allowed by your application")

        }

    }


}

2 Answers2

3

Make your bluetoothManager an instance variable. Currently it gets deallocated when viewDidLoad() finishes.

Jakub Vano
  • 3,833
  • 15
  • 29
  • Are you sure it is not being called? You are not checking all the states in there. btw switch statement would be more appropriate there – Jakub Vano Apr 23 '15 at 09:11
2

You have to save your bluetoothManager to global variable of your ViewController in order to receive call-backs.

class AppSettingsViewController: UIViewController, CBPeripheralManagerDelegate {

    var bluetoothManager : CBPeripheralManager?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)


    }
}
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • still not calling ?? is it that, it gets called whenever there is change on the bluetooth settings ? –  Apr 23 '15 at 09:05
  • Are you running on a real device (iPhone 4S or later)? You should get a call to `peripheralManagerDidUpdateState` shortly after you initialise the `CBPeripheralManager` if Bluetooth is on – Paulw11 Apr 23 '15 at 09:07
  • I'm not sure about extension's effect, but just try to move delegate call to the Viewcontroller but not in extension. This call-back is called after your create (and keep reference) a CBPeripheralManager and whenever the bluetooth of your iDevice change state. – Duyen-Hoa Apr 23 '15 at 09:10
  • oops..i am testing it on simulator.. i thought it should call delegate..delegate is called only after state is changed of the bluetooth? –  Apr 23 '15 at 09:11
  • 1
    You can't test Bluetooth on a simulator - only on a real device with BLE support. This code is working on my phone - https://gist.github.com/paulw11/1ec2077b2d868d48f337 – Paulw11 Apr 23 '15 at 09:12
  • The call-back is still called even in simulator. But you will received an Unsupported state – Duyen-Hoa Apr 23 '15 at 09:13
  • I updated the gist to include a switch statement for all values - unsupported is reported on the simulator – Paulw11 Apr 23 '15 at 09:34
  • it is not the case... why this method isnot calling func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) { –  Apr 23 '15 at 10:16