LifecycleScope - heartRateBpm - how to make stop and start function?

Hello there,

I have simple code for getting BPM rate (wear OS - watch app - kotlin):

fun onCreate(savedInstanceState: Bundle?)
{
...
   // Bind viewmodel state to the UI.
        lifecycleScope.launchWhenStarted {
            viewModel.uiState.collect {
                updateViewVisiblity(it)
            }
        }
        /*
        lifecycleScope.launchWhenStarted {
            viewModel.heartRateAvailable.collect {
                //binding.textView3.text = getString(R.string.measure_status, it)
            }
        }
        */

        lifecycleScope.launchWhenStarted {
            viewModel.heartRateBpm.collect {
                binding.lastMeasuredValue.text = String.format("%.0f", it)
            }
        }
...
}

This work, when I open my app. I just need create button with start / stop function. Can someone give me a hint how to? :slight_smile:

Roman