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

问题描述 投票:1回答:1

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

感谢您的帮助,

ios swift bluetooth cbperipheral
1个回答
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")
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.