CBCentralManager iOS10 and iOS9

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:

  • JSON Parsing in Objective-C
  • Can't change UITextView frame size programatically
  • Set a default font for whole iOS app?
  • Draw a path with variable width in iOS
  • Load image from CocoaPods resource bundle
  • How can I create a custom UIActivity in iOS?
  • 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?

    4 Solutions Collect From Internet About “CBCentralManager iOS10 and iOS9”

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

    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.

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

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