1
import UIKit
import CoreBluetooth
import SciChart

let maestroBrainDataCBUUID = CBUUID(string: "49535343-1E4D-4BD9-BA61-23C647249616")

class HRMViewController: UIViewController {
    var sciChartSurface: SCIChartSurface?
    var wave:Double! = 0.0

    var lineDataSeries: SCIXyDataSeries!
    var scatterDataSeries: SCIXyDataSeries!

    var lineRenderableSeries: SCIFastLineRenderableSeries!
    var scatterRenderableSeries: SCIXyScatterRenderableSeries!

    var timer: Timer?
    var phase = 0.0
    var i = 0

    @IBOutlet weak var brainRateLabel: UILabel!

wave is meaning of bluetooth data as realtime.

    var centralManager: CBCentralManager!
    var maestroPeripheral:CBPeripheral!

    override func viewDidLoad() {
        super.viewDidLoad()

        <elide>
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if nil == timer{
            timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: updatingDataPoints)
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if nil != timer{
            timer?.invalidate()
            timer = nil
        }
    }

    func updatingDataPoints(timer:Timer){
        i += 1
        if wave != nil {
            lineDataSeries.appendX(SCIGeneric(i), y: SCIGeneric((wave)))
        }

        phase += 0.01

        sciChartSurface?.zoomExtents()
        sciChartSurface?.invalidateElement()
    }

    func createDataSeries(){
        lineDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
        lineDataSeries.fifoCapacity = 500
        lineDataSeries.seriesName = "line series"

        for i in 0...500{
            lineDataSeries.appendX(SCIGeneric(i), y: SCIGeneric(wave))
        }

        i = Int(lineDataSeries.count())
    }

I tried to make realtime chart using sci-chart framework. But if I try to run coding, I get the error:

"Unexpectedly found nil while unwrapping an Optional value"

How to solve this problem? Is there anyone who have experience like as me TT?

extension HRMViewController: CBCentralManagerDelegate {
  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    switch central.state {

    <elide>
  }
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic,
                  error: Error?) {
    switch characteristic.uuid {
      case maestroBrainDataCBUUID:
        wave = Double(brainData(from: characteristic))*3.3/65536

      default:
      print("Unhandled Characteristic UUID: \(characteristic.uuid)")
    }
  }

I used it to chart a wave value, but I got an error when I stripped off the option. So if we use if to make the line chart appear if it is not nil, then there was an error, and I tried several times and got a runtime error. What should I do?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
이상호
  • 29
  • 2

1 Answers1

1

I think it could be caused by missing createDataSeries() call in your code. So when updatingDataPoints() is called lineDataSeries instance isn't initialized.

Try adding createDataSeries() call before updateDataPoints() will be called or at least add nil check for lineDataSeries instance in updateDataPoints().

Yura Khariton
  • 484
  • 2
  • 6