R3DC0DE
R3DC0DE

Reputation: 39

How to properly get heart rate bpm in a Watch Face Service?

I made a watch face using the Jetpack Watch Face API and I'm trying to get the bpm data into a string as to paint them into the canvas. I don't want to use complication as it seems that they will go away sometime in the futur (and only useable from the Watch Face Format API). But I also don't know if I should get the data from the sensor directly or from the Health Service API.

I tried various methods since there is no clear indication on a correct way to do this. I settled with passive monitoring using the Health Service API. But it doesn't seems to work : the data isn't updating accordingly to what the bpm was set to in the emulator. In fact, the onNewDataPointsReceived function is never called. However, there isn't any error showing in the logcat, so I'm wondering if I'm doing something wrong here. I'm also wondering if that is the correct way to fetch that kind of data.

Here is my code (reduced to the minimum to be comprehensible).

class RisingSunCanvasRenderer( /* nothing fancy here */) : Renderer.CanvasRenderer2<RisingSunCanvasRenderer.CSharedAssets>(/* nothing fancy here */) {
 private val healthClient = HealthServices.getClient(context)
    private val passiveMonitoringClient = healthClient.passiveMonitoringClient
    object HeartRate {
        private var bpm = 0.0f;

        fun setBpm(newBpm: Float) {
            bpm = newBpm
        }
        fun getBpm(): Float {
            return bpm
        }
    }

    class PassiveDataService : PassiveListenerService() { // Listener Service
        override fun onNewDataPointsReceived(dataPoints: DataPointContainer) {
            val data = dataPoints.getData(DataType.HEART_RATE_BPM)
            HeartRate.setBpm(data.last().value.toFloat())

            Log.d("HEARTBEAT", "value updated")
        }
    }

    init { // I'm creating the listener on init of the renderer
        val passiveListenerConfig = PassiveListenerConfig.builder()
            .setDataTypes(setOf(DataType.HEART_RATE_BPM))
            .build()

        passiveMonitoringClient.setPassiveListenerServiceAsync(
            PassiveDataService::class.java,
            passiveListenerConfig
        )

        Log.d("MONITORING", "Init monitoring completed")
    }

    override fun render(canvas: Canvas, bounds: Rect, zonedDateTime: ZonedDateTime, sharedAssets: CSharedAssets) {
        canvas.drawColor(Color.BLACK)
        canvas.drawText(HeartRate.getBpm().toString(), bounds.centerX().toFloat(), bounds.centerY() + 100f, paint)
    }
}

Upvotes: 1

Views: 188

Answers (1)

claired4l
claired4l

Reputation: 64

This is also news to me but now you can use the Health Services Sensor Panel to simulate health sensors! https://developer.android.com/health-and-fitness/guides/health-services/simulated-data#use_the_health_services_sensor_panel

Upvotes: 1

Related Questions