while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)
while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)
早些时候,我 post 编辑了一个与此类似的关于服务问题的 post,我通过添加 peripheral.delegate = self.
修复了它
现在我在尝试发现服务的特征时遇到了这个问题。
while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error:
这是我的ViewController
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)
}
}
}
}
有人知道我该怎么做吗?谢谢!
您将 peripheral(_:didDiscoverCharacteristicsFor:error:)
函数 放在 函数 peripheral(_:didDiscoverServices:)
中。像这样,它是一个嵌套函数,而不是 CBPeripheralDelegate
扩展的一部分。将它移出函数并移入适当的扩展中,它应该可以工作。
早些时候,我 post 编辑了一个与此类似的关于服务问题的 post,我通过添加 peripheral.delegate = self.
修复了它现在我在尝试发现服务的特征时遇到了这个问题。
while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error:
这是我的ViewController
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)
}
}
}
}
有人知道我该怎么做吗?谢谢!
您将 peripheral(_:didDiscoverCharacteristicsFor:error:)
函数 放在 函数 peripheral(_:didDiscoverServices:)
中。像这样,它是一个嵌套函数,而不是 CBPeripheralDelegate
扩展的一部分。将它移出函数并移入适当的扩展中,它应该可以工作。