didDiscoverPeripheral"未能建立"错误

时间:2015-07-23 01:47:58

标签: ios swift core-bluetooth

我不确定为什么这段代码无法构建,错误信息似乎非常神秘。

代码:

var centralManager: CBCentralManager!;
var nrf8001Peripheral: CBPeripheral!;

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // initialize centralManager
    self.centralManager = CBCentralManager(delegate: self, queue: nil);

    // start scanning for device
    self.centralManager.scanForPeripheralsWithServices([UART_SERVICE_UUID], options:nil);
}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData advertisementData: [NSObject : AnyObject]!, RSSI RSSI: NSNumber) {

            //print out the name of the scanned peripheral
            print("Discovered \(peripheral.name)")

            //print out the UUID of the scanned peripheral
            print("NSUUID string \(peripheral.identifier.UUIDString)")

            //stop scanning when found
            self.centralManager.stopScan()

            //connect when found
            self.centralManager.connectPeripheral(peripheral, options:nil);
}

我从XCode编译器收到的错误是:

" Objective-C方法&#central; didManager:didDiscoverPeripheral:advertisementData:RSSI:'方法' centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)'与可选的需求方法和中央管理器(:didDiscoverPeripheral:advertisementData:RSSI:)'冲突。在协议' CBCentralManagerDelegate'"

通过查看CoreBluetooth文档,似乎方法语法和参数是正确的,参数的可选性直接从规格表中复制:https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/#//apple_ref/occ/intfm/CBCentralManagerDelegate/centralManager:didDiscoverPeripheral:advertisementData:RSSI

任何帮助将不胜感激!谢谢

根据评论:

  1. 使用XCode 7 beta
  2. 当我将函数声明更改为:

    func centralManager(中央:CBCentralManager,didDiscoverPeripheral peripheral:CBPeripheral,advertisementData advertisementData:[NSObject:AnyObject],RSSI RSSI:NSNumber)

  3. 我仍然遇到相同的构建错误。

    1. 我的centralManagerDidUpdateState:方法是

      func centralManagerDidUpdateState(central: CBCentralManager) {
      
      print("centralManagerDidUpdateState:");
      
      switch (central.state) {
      
          case .PoweredOff:
              print("CBCentralManagerStatePoweredOff");
      
          case .Resetting:
              print("CBCentralManagerStateResetting");
      
          case .PoweredOn:
              print("CBCentralManagerStatePoweredOn");
      
          //scan for peripheral devices
          self.centralManager.scanForPeripheralsWithServices([UART_SERVICE_UUID], options:nil);
      
          case .Unauthorized:
              print("CBCentralManagerStateUnauthorized");
      
          case .Unsupported:
              print("CBCentralManagerStateUnsupported");
      
          default:
              print("CBCentralManagerStateUnknown");
          }
      }
      

1 个答案:

答案 0 :(得分:2)

感谢您的建议;我最终通过XCode 7文档找到了答案。以下函数的XCode 6语法如下:

func centralManagerDidUpdateState(central: CBCentralManager!) {}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData advertisementData: [NSObject : AnyObject]!, RSSI RSSI: NSNumber) {}

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {}

func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {}

但是,这些函数将与XCode 7 CoreBluetooth库声明冲突。

请注意选项以及数据类型的不同用法。

(XCode 6) error:NSError! vs。 (XCode 7) error:NSError?

(XCode 6) advertisementData : [NSObject : AnyObject]! vs。 (XCode 7) advertisementData [String : AnyObject]

XCode 7 beta的相应函数声明实际上如下:

func centralManagerDidUpdateState(central: CBCentralManager) {}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {}

func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {}

希望这对有其他问题的人有所帮助!