CBCentralManager iOS10 and iOS9

后端 未结 4 1638
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 21:01

So I\'m migrating to iOS10 but I also need my code to run on iOS9. I\'m using CoreBluetooth and CBCentralManagerDelegate. I can get my code to work for iOS10 however I need

4条回答
  •  时光取名叫无心
    2021-01-04 21:43

    I contacted Apple about this and was given the following response (paraphrasing).

    Due to the changing nature of swift, the above implementation is not possible however you can use the rawValue of the enum as the state is identical between the two classes. Therefore the following will work for now:

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if #available(iOS 10.0, *) {
            switch central.state{
            case CBManagerState.unauthorized:
                print("This app is not authorised to use Bluetooth low energy")
            case CBManagerState.poweredOff:
                print("Bluetooth is currently powered off.")
            case CBManagerState.poweredOn:
                print("Bluetooth is currently powered on and available to use.")
            default:break
            }
        } else {
            // Fallback on earlier versions
            switch central.state.rawValue {
            case 3: // CBCentralManagerState.unauthorized :
                print("This app is not authorised to use Bluetooth low energy")
            case 4: // CBCentralManagerState.poweredOff:
                print("Bluetooth is currently powered off.")
            case 5: //CBCentralManagerState.poweredOn:
                print("Bluetooth is currently powered on and available to use.")
            default:break
            }
        }
    }
    

提交回复
热议问题