而委托为 nil 或未实现外围设备:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)

Posted

技术标签:

【中文标题】而委托为 nil 或未实现外围设备:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)【英文标题】:while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift) 【发布时间】:2020-04-26 07:14:30 【问题描述】:

之前我发布了一篇与此类似的关于服务问题的帖子,我通过添加 peripheral.delegate = self 解决了这个问题。

现在,当我尝试发现服务的特征时,我遇到了这个问题。

当委托为 nil 或未实现外设时:didDiscoverCharacteristicsForService:error:

这是我的视图控制器

import UIKit
import CoreBluetooth


let rowerServiceCBUUID = CBUUID(string: "CE060000-43E5-11E4-916C-0800200C9A66")

class HRMViewController: UIViewController 
  @IBOutlet weak var heartRateLabel: UILabel!
  @IBOutlet weak var bodySensorLocationLabel: UILabel!
  var centralManager: CBCentralManager!
  var pmPeripheral: CBPeripheral!
  var wattValue: Int!
  override func viewDidLoad() 
    super.viewDidLoad()
    centralManager = CBCentralManager(delegate: self, queue: nil)
    // Make the digits monospaces to avoid shifting when the numbers change
    heartRateLabel.font = UIFont.monospacedDigitSystemFont(ofSize: heartRateLabel.font!.pointSize, weight: .regular)
  

  func onHeartRateReceived(_ heartRate: Int) 
    heartRateLabel.text = String(heartRate)
    print("BPM: \(heartRate)")
  

extension HRMViewController: CBCentralManagerDelegate 
  func centralManagerDidUpdateState(_ central: CBCentralManager) 
    switch central.state 
    case .unknown:
      print("central.state is .unknown")
    case .resetting:
      print("central.state is .resetting")
    case .unsupported:
      print("central.state is .unsupported")
    case .unauthorized:
      print("central.state is .unauthorized")
    case .poweredOff:
      print("central.state is .poweredOff")
    case .poweredOn:
      print("central.state is .poweredOn")
      centralManager.scanForPeripherals(withServices: [rowerServiceCBUUID])
    @unknown default: break

    
  

  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) 
    print(peripheral)
    peripheral.delegate = self
    pmPeripheral = peripheral
    pmPeripheral.delegate = self
    centralManager.stopScan()
    centralManager.connect(pmPeripheral!)
  
  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) 
    print("Connected!")
    pmPeripheral.discoverServices(nil)
  


extension HRMViewController: CBPeripheralDelegate 
  func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) 
    guard let services = peripheral.services else  return 
    peripheral.delegate = self
    for service in services 
      print(service)
      print(service.characteristics ?? "characteristics are nil")
      peripheral.discoverCharacteristics(nil, for: service)
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
      peripheral.delegate = self
      guard let characteristics = service.characteristics else  return 
      for characteristic in characteristics 
        print(characteristic)
      
    
  

有人知道我应该怎么做吗?谢谢!

【问题讨论】:

【参考方案1】:

您将peripheral(_:didDiscoverCharacteristicsFor:error:) 函数放入 peripheral(_:didDiscoverServices:) 函数中。像这样,它是一个嵌套函数,而不是 CBPeripheralDelegate 扩展的一部分。将其移出函数并适当地进入扩展,它应该可以工作。

【讨论】:

以上是关于而委托为 nil 或未实现外围设备:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)的主要内容,如果未能解决你的问题,请参考以下文章

未调用 BLE 外设委托

在后台扫描外围设备

如何使用标识符连接外围设备/ BLE 设备?

如何使用标识符连接外围设备/ BLE设备?

XCode:如何通过 UUID 连接到 BLE 外围设备?

在 prepareForSeque 时委托不是 nil,但在导航表视图中调用时为 nil