Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect is the Bluetooth of the user's iPhone Off or On?

I am trying to detect is the Bluetooth of the user's iPhone On or Off. If it is Off I want to send a notification to the user to turn it On. So far I have done this:

import CoreBluetooth

  class ViewController: UIViewController, CLLocationManagerDelegate,AVCaptureMetadataOutputObjectsDelegate,CBManager {   

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

  }

func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
    print(#function)
    if peripheral.state == CBManagerState.poweredOn {
        print("Broadcasting...")

    //    myBTManager!.startAdvertising(_broadcastBeaconDict)
    } else if peripheral.state == CBManagerState.poweredOff {
        print("Stopped")
        myBTManager!.stopAdvertising()
    } else if peripheral.state == CBManagerState.unsupported {
        print("Unsupported")
    } else if peripheral.state == CBManagerState.unauthorized {
        print("This option is not allowed by your application")
    }
}

But as You can see from the picture, something is wrong.enter image description here

Would you please help me how to fix this issue, I am new to swift and CoreBluetooth technology. I am also using Reachability for detecting the Wi-Fi connection, so if it also works for Bluetooth, I would prefer to use Reachability then.

like image 852
Ivan Avatar asked Dec 18 '16 12:12

Ivan


People also ask

Can someone connect to my iPhone Bluetooth without me knowing?

Can someone connect to my Bluetooth without me knowing? Theoretically, anyone can connect to your Bluetooth and gain unauthorized access to your device if the visibility of your Bluetooth device is on.

Can you detect Bluetooth device?

To find an active Bluetooth device, first make sure you have Bluetooth enabled on your smartphone. Next, download Wunderfind for your iPhone or Android device and launch the app. Immediately, you'll see a list of Bluetooth devices that your smartphone has detected using its built-in Bluetooth radio.


1 Answers

  1. You should be implementing the protocol CBPeripheralManagerDelegate, so replace CBManager in your class definition line with CBPeripheralManagerDelegate.

  2. In Swift 3, the signature of peripheralManagerDidUpdateState is now:

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
    
  3. You can't initialize the CBPeripheralManager at class creation time since self is only available after the class has been initialized. Instead, make your property:

    var myBTManager: CBPeripheralManager?
    

    and initialize it in viewDidLoad:

    override func viewDidLoad() {
        ...       
        myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
        ...
    }
    
like image 54
vacawama Avatar answered Sep 22 '22 13:09

vacawama