我实际上正在开发一个swift3项目,它需要知道蓝牙是否在设备上激活。我已经阅读了很多关于这个主题的主题,但是没有一个可以帮助我通过另一种方式来做,而不是使用centralManagerDidUpdateState
其实我有这个代码:
private func GetBluetoothInformation() {
let options = [CBCentralManagerOptionShowPowerAlertKey:0]
bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
var status: Bool = false
switch peripheral.state {
case .poweredOn:
status = true
default:
status = false
}
m_session_chofer?.setIsBluetooth(status)
}
但通过这样做,我不知道每次调用GetBluetoothInformation
是否会正确更新状态。
感谢您的帮助,
var btManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
println(__FUNCTION__)
if peripheral.state == CBPeripheralManagerState.PoweredOn {
println("Broadcast")
btManager!.startAdvertising(_broadcastBeaconDict)
} else if peripheral.state == CBPeripheralManagerState.PoweredOff {
println("Stop")
btManager!.stopAdvertising()
} else if peripheral.state == CBPeripheralManagerState.Unsupported {
println("Unsupport")
} else if peripheral.state == CBPeripheralManagerState.Unauthorized {
println("Not allow")
}
}