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.
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.
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.
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.
You should be implementing the protocol CBPeripheralManagerDelegate
, so replace CBManager
in your class
definition line with CBPeripheralManagerDelegate
.
In Swift 3, the signature of peripheralManagerDidUpdateState
is now:
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
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)
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With