Earlier I posted a similar post to this one about a services problem and I got it fixed by adding peripheral.delegate = self.
Now I'm having this problem when I try to discover characteristics for the services.
while delegate is either nil or does not implement peripheral:didDiscoverCharacteristicsForService:error:
Here is my 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)
}
}
}
}
Does anyone know what I should do? thanks!
You put your peripheral(_:didDiscoverCharacteristicsFor:error:)
function inside the peripheral(_:didDiscoverServices:)
function. Like this it's a nested function and not part of the CBPeripheralDelegate
extension. Move it outside of the function and into the extension proper and it should work.