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
Код: Выделить всё
onNewDataPointsReceivedHere is my code (reduced to the minimum to be comprehensible).
Код: Выделить всё
class RisingSunCanvasRenderer( /* nothing fancy here */) : Renderer.CanvasRenderer2(/* 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)
}
}
Источник: https://stackoverflow.com/questions/781 ... ce-service
Мобильная версия