Как правильно получить частоту пульса в минуту в службе Watch Face?Android

Форум для тех, кто программирует под Android
Ответить
Гость
 Как правильно получить частоту пульса в минуту в службе Watch Face?

Сообщение Гость »


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(/* 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»