0

I want to check if Bluetooth is enabled on my device and print the status to the console. My class conforms to CBPeripheralManagerDelegate and I have no errors. I read the documentation and I implemented the peripheralManagerDidUpdateState function but it never gets called. My understanding is that function is called when Bluetooth's availability changes. So if I turn Bluetooth off that function should be called and then should be called again once I turn Bluetooth back on. But that doesn't happen. I saw another question that had the same problem but the solution was written in Objective-C and didn't solve my problem.

When I run this code in viewDidLoad it returns unknown and I don't know why. I expected it to return powered on:

cbManager.delegate = self
    if cbManager.state == .PoweredOn {
        println("powered on")
    } else if cbManager.state == .Unauthorized {
        println("unauthorized")
    } else if cbManager.state == .Unknown {
        println("unknown")
    } else if cbManager.state == .PoweredOff {
        println("pow off")
    } else if cbManager.state == .Unsupported {
        println("unsupported")
    }

I declare cbManager outside of viewDidLoad because if I don't the peripheralManagerDidUpdateState doesn't recognize the manager. That other question's problem was "the object myPeripheralManager is deallocated as soon as viewDidLoad method returns". I think that's my problem too but I don't know how to fix it.

Here's my manager declaration: let cbManager = CBPeripheralManager() which is outside of viewDidLoad.

Thanks!

UPDATE:

After many hours I think I have found the problem. I don't know if this is why it didn't work but it works now. I was using a CBPeripheralManager when I should've been using a CBCentralManager. This question helped show me (even though it was a completely different problem) the difference between a central and peripheral manager.

UPDATE #2:

Also, the manager that was being passed into peripheralManagerDidUpdateState was different than the manager I was using in the function itself. This in combination with using the wrong manager caused the problems. Hope this helps someone!

Community
  • 1
  • 1
Jake
  • 319
  • 1
  • 7
  • 21

0 Answers0