2016-09-19 69 views
1

我正在使用CoreBluetooth中央管理器的iOS应用程序。该应用程序按预期工作,直到我更新到xCode 8.此更新强制我将转换管理器的代码从Swift 2.3转换为Swift 3.0。 之后,我收到错误消息'无法将Type'CBManagerState'的值转换为期望的参数类型'CBCentralManagerState',并且我正在搜索答案,但由于更新是新的原因,因此没有任何有用的问题或有关与Swift 3.0或iOS 10.0一起使用的CB蓝牙的文档。如何在转换为Swift 3.0语法后将类型'CBManagerState'的值转换为预期类型'CBCentralManagerState'?

标有星号的行是产生错误的行。

final class BluetoothSerial: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { 
    ....//some code here from HM10 Bluetooth Serial 
    var centralManager: CBCentralManager! 
    var state: CBCentralManagerState { get { return centralManager.state } * 

    func centralManagerDidUpdateState(_ central: CBCentralManager) { 
     //note that "didDisconnectPeripheral" won't be called if BLE is turned off while connected 

     connectedPeripheral = nil 
     pendingPeripheral = nil 

     //send it to the delegate 
     delegate.serialDidChangeState(central.state) * 
    } 
} 

任何帮助表示赞赏。 在此先感谢。

+0

你能试图读取文档中关于BLE前面CBCentralManager? [docs](https://developer.apple.com/reference/corebluetooth/cbcentralmanagerstate) –

+0

第三行中央管理器的类型是什么?并且delegate.serialDidChangeState()期望什么参数类型? –

+0

是的,我阅读文档,这是如何让它在更新之前完美运行的。 centralManager的类型是'CBCentralManager!'。 delegate.serialDidChangeState()的预期参数类型是'CBCentralManagerState'。这是我用来在更新之前使其工作的基本源代码:https://github.com/hoiberg/HM10-BluetoothSerial-iOS Thaaaanks;) – iiiiirev

回答

4

这编译对我来说:

var state: CBCentralManagerState { get { return CBCentralManagerState(rawValue: centralManager.state.rawValue)! } 

按照dev forums

的枚举是二进制兼容,所以你的代码可以在任何的iOS版本

我运行正常m只使用 centralManagerDidUpdateState函数中的状态 - 但是这样做如下:

switch central.state{ 
    case .poweredOn: 
     NSLog("CoreBluetooth BLE hardware is powered on"); 
     break 
    case .poweredOff: 
     NSLog("CoreBluetooth BLE hardware is powered off"); 
     break; 
    case .unauthorized: 
     NSLog("CoreBluetooth BLE state is unauthorized"); 
     break 
    case .unknown: 
     NSLog("CoreBluetooth BLE state is unknown"); 
     break; 
    case .unsupported: 
     NSLog("CoreBluetooth BLE hardware is unsupported on this platform"); 
     break; 
    default: 
     break 
    } 

,编译器似乎很乐​​意与(即 - 去除CBCentralManager.poweredOn

+0

迄今为止,这对我有效。是的,我听说它在iOS 10中贬值,但我不知道如何替换它或解决此问题。 非常感谢,你救了我的生命(; – iiiiirev

+0

真棒!没问题 - 如果它为你修复 - 请upvote /接受答案!=] – Andrew

相关问题