不使用CBPeripheralManager获取蓝牙ON / OFF的状态

时间:2017-02-08 00:31:03

标签: ios swift bluetooth cbperipheral

我实际上正在开发一个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是否会正确更新状态。

感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

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")
    }
 }