无论设备如何,我都需要知道蓝牙是否开启/关闭

时间:2017-07-09 07:04:22

标签: ios swift core-bluetooth

因此,当我在iPad上运行此片段时,我正在尝试检查iOS设备上的蓝牙是打开还是关闭当我打开/关闭蓝牙时,它会打印Bluetooth Status: Turned OnBluetooth Status: Turned Off 。但是当我在iPad 2上运行它时会打印Bluetooth Status: Not Supported

fileprivate var bluetoothPeripheralManager: CBPeripheralManager?

override init() {
    super.init()
    let options = [CBCentralManagerOptionShowPowerAlertKey:0]
    bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
}

extension PrintersMonitor: CBPeripheralManagerDelegate{
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager){
    var statusMessage = ""

    switch peripheral.state {
    case .poweredOn:
        statusMessage = "Bluetooth Status: Turned On"
    case .poweredOff:
        statusMessage = "Bluetooth Status: Turned Off"
    case .resetting:
        statusMessage = "Bluetooth Status: Resetting"
    case .unauthorized:
        statusMessage = "Bluetooth Status: Not Authorized"
    case .unsupported:
        statusMessage = "Bluetooth Status: Not Supported"
        bluetoothPeripheralManager = nil
    default:
        statusMessage = "Bluetooth Status: Unknown"
    }

    log(statusMessage)

    if peripheral.state == .poweredOn{
        if state != .searching{
            resume()
        }
    }else if peripheral.state == .poweredOff{
        pause(isPrinting: false)
    }
}

}

我身边是否缺少一些东西! 我需要知道蓝牙是否开启/关闭,无论设备如何!

1 个答案:

答案 0 :(得分:0)

iPad 2不支持BLE,这是Core Bluetooth公开的蓝牙。这就是您获得“不支持”状态的原因。

没有提供传统蓝牙电源状态的API。