CBCentralManager iOS10 and iOS9

痴心易碎 提交于 2019-12-18 21:18:05

问题


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 the fallback to work for iOS9 as well.

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{
        case CBCentralManagerState.unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case CBCentralManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case CBCentralManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    }
}

I get the error:

Enum case 'unauthorized' is not a member of type 'CBManagerState'

On the line:

case CBCentralManagerState.unauthorized: 

As well as for .poweredOff and .poweredOn.

Any ideas how I can get it to work in both cases?


回答1:


You can simply omit the enumeration type name and just use the .value. This will compile without warnings and works on iOS 10 and earlier since the underlying raw values are compatible.

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state{
        case .unauthorized:
            print("This app is not authorised to use Bluetooth low energy")
        case .poweredOff:
            print("Bluetooth is currently powered off.")
        case .poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
}



回答2:


I worked around this issue on Xcode 8 with Swift 2.3 (targeting iOS 8 and above) by creating an extension property on CBCentralManager which is of the old enum type, CBCentralManagerState. I named it centralManagerState. I refer to CBCentralManager.centralManagerState where I used to refer to CBCentralManager.state.

extension CBCentralManager {

    internal var centralManagerState: CBCentralManagerState  {
        get {
            return CBCentralManagerState(rawValue: state.rawValue) ?? .Unknown
        }
    }
}

I got the idea from this forum thread though they hadn't posted the code yet.




回答3:


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



回答4:


func centralManagerDidUpdateState(central: CBCentralManager) {

    if #available(iOS 10.0, *)
    {
        switch (central.state) {

        case CBManagerState.PoweredOff:
            print("CBCentralManagerState.PoweredOff")

        case CBManagerState.Unauthorized:
            // Indicate to user that the iOS device does not support BLE.
            print("CBCentralManagerState.Unauthorized")
            break

        case CBManagerState.Unknown:
            // Wait for another event
            print("CBCentralManagerState.Unknown")
            break

        case CBManagerState.PoweredOn:
            print("CBCentralManagerState.PoweredOn")

            self.centralManager!.scanForPeripheralsWithServices([CBUUID(string:TRANSFER_UUID)], options:[CBCentralManagerScanOptionAllowDuplicatesKey: false])

        case CBManagerState.Resetting:
            print("CBCentralManagerState.Resetting")

        case CBManagerState.Unsupported:
            print("CBCentralManagerState.Unsupported")
            break
        }
    }
    else
    {

       switch central.state.rawValue
       {
        case 0: // CBCentralManagerState.Unknown
            print("CBCentralManagerState.Unknown")
        break

        case 1: // CBCentralManagerState.Resetting
        print("CBCentralManagerState.Resetting")


        case 2:// CBCentralManagerState.Unsupported
            print("CBCentralManagerState.Unsupported")
        break

        case 3: // CBCentralManagerState.unauthorized
            print("This app is not authorised to use Bluetooth low energy")
        break

        case 4: // CBCentralManagerState.poweredOff:
            print("Bluetooth is currently powered off.")

        case 5: //CBCentralManagerState.poweredOn:
            self.centralManager!.scanForPeripheralsWithServices([CBUUID(string:TRANSFER_UUID)], options:[CBCentralManagerScanOptionAllowDuplicatesKey: false])
            print("Bluetooth is currently powered on and available to use.")

        default:break
        }

    }

}


来源:https://stackoverflow.com/questions/39450534/cbcentralmanager-ios10-and-ios9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!