如何解决 [CoreBluetooth] API MISUSE: 只能在 swift 4 的开机状态下接受此命令?

mab*_*mab 6 xcode core-bluetooth swift swift3 xcode9-beta

我创建了一个 iOS 应用程序来搜索最近的蓝牙设备。但是当我在 iPhone 上运行这个应用程序时,出现以下错误

[CoreBluetooth] API MISUSE:只能在开机状态下接受此命令

我该如何解决此错误?为什么应用程序中会出现此错误?提前致谢

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print(central.state)
    switch (central.state) {
    case CBManagerState.poweredOff:
       print("CoreBluetooth BLE hardware is powered off")
        break;
    case CBManagerState.unauthorized:
        print("CoreBluetooth BLE state is unauthorized")
        break

    case CBManagerState.unknown:
         print("CoreBluetooth BLE state is unknown");
        break

    case CBManagerState.poweredOn:
        print("CoreBluetooth BLE hardware is powered on and ready")
        bluetoothAvailable = true;
        break;

    case CBManagerState.resetting:
         print("CoreBluetooth BLE hardware is resetting")
        break;
    case CBManagerState.unsupported:
          print("CoreBluetooth BLE hardware is unsupported on this platform");
        break
    }
    if bluetoothAvailable == true
    {
        discoverDevices()
    }

}

func discoverDevices() {
    print("Discovering devices")
    manager?.scanForPeripherals(withServices: nil, options: nil)
    //centralManager.scanForPeripheralsWithServices(nil, options: nil)

}
Run Code Online (Sandbox Code Playgroud)