CBCentralManager iOS10 和 iOS9

CBCentralManager iOS10 and iOS9

所以我要迁移到 iOS10,但我还需要我的代码到 iOS9 上的 运行。我正在使用 CoreBluetooth 和 CBCentralManagerDelegate。我可以让我的代码为 iOS10 工作,但是我需要回退也为 iOS9 工作。

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

我收到错误:

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

在线:

case CBCentralManagerState.unauthorized: 

以及 .poweredOff 和 .poweredOn。

我有什么想法可以让它在这两种情况下都起作用吗?

我在 Xcode 8 和 Swift 2.3(针对 iOS 8 及更高版本)上通过在 CBCentralManager 上创建扩展 属性 解决了这个问题这是旧的枚举类型,CBCentralManagerState。我把它命名为centralManagerState。我指的是 CBCentralManager.centralManagerState 我以前指的是 CBCentralManager.state.

extension CBCentralManager {

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

我从 this forum thread 那里得到了灵感,尽管他们还没有发布代码。

我就此联系了 Apple,得到了以下回复(释义)。

由于 swift 的变化性质,上述实现是不可能的,但是您可以使用枚举的 rawValue,因为两者之间的状态相同 类。因此,以下内容暂时有效:

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

您可以简单地省略枚举类型名称,只使用 .value。这将在没有警告的情况下编译并适用于 iOS 10 及更早版本,因为底层原始值是兼容的。

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

func centralManagerDidUpdateState(中央: 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
        }

    }

}