如何在 Android Wearable 中访问心率传感器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26489281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:35:41  来源:igfitidea点击:

How to access heart rate sensor in Android Wearable?

androidwear-osmoto-360

提问by wonglik

I am having problems accessing heart rate sensor on Moto 360.

我在访问 Moto 360 上的心率传感器时遇到问题。

I tried following things :

我尝试了以下事情:

Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);

and then implement SensorEventListener interface :

然后实现 SensorEventListener 接口:

@Override
public void onSensorChanged(SensorEvent event) {

        String TAG = "tag";
        Log.i(TAG, "--------------------------");
        Log.i(TAG, msg);
        Log.i(TAG, ""+ event.sensor.getType());
        Log.i("live","--------------");

And what is strange to me I do not get any messages at all (not only heart rate).

对我来说奇怪的是我根本没有收到任何消息(不仅是心率)。

Also I tried listing all sensors and it does not show Heart rate sensor on the list.

我还尝试列出所有传感器,但列表中没有显示心率传感器。

Of course I've added persmissions

当然我已经添加了权限

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.watchtest" >

<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.BODY_SENSORS" />

Any ideas ?

有任何想法吗 ?

thanks.

谢谢。

w.

w.

采纳答案by wonglik

As @Kent and @Murphy suggested, updated SDK was the solution. In my case I needed to drop the project and create new from scratch as even with updated SDK old one did not work.

正如@Kent 和@Murphy 所建议的,更新的SDK 是解决方案。就我而言,我需要放弃该项目并从头开始创建新项目,因为即使使用更新的 SDK 旧版本也无法正常工作。

回答by Alexander K

Started to work for me after I did the following:

在我执行以下操作后开始为我工作:

  1. Uninstalled my app from the watch with

    adb -s localhost:4444 uninstall com.example.android.wearable.jumpingHyman
    
  2. Added permissions to get the heart rate sensor

    <uses-permission android:name="android.permission.BODY_SENSORS"/>
    
  3. Set the min and target SDK version to match the watch

    android:minSdkVersion="20" android:targetSdkVersion="20"
    
  1. 从手表上卸载我的应用程序

    adb -s localhost:4444 uninstall com.example.android.wearable.jumpingHyman
    
  2. 添加了获取心率传感器的权限

    <uses-permission android:name="android.permission.BODY_SENSORS"/>
    
  3. 设置最小和目标 SDK 版本以匹配手表

    android:minSdkVersion="20" android:targetSdkVersion="20"
    

Started the app again. I received the heart rate sensor with Sensor.TYPE_HEART_RATE and I started to receive its readings. Although they were far from good. There were a lot of readings, but they were just the same, limited to these 5 values:

再次启动应用程序。我收到了带有 Sensor.TYPE_HEART_RATE 的心率传感器,并开始接收它的读数。虽然他们差得很远。有很多读数,但它们都一样,仅限于这 5 个值:

heartRate onSensorChanged values = [0.0], accuracy = 0, sensor = {Sensor name="Heart Rate Sensor", vendor="Motorola", version=1, type=21, maxRange=65535.0, resolution=1.0, power=0.45, minDelay=0}
heartRate onSensorChanged values = [53.0], accuracy = 2, sensor = {Sensor name="Heart Rate Sensor", vendor="Motorola", version=1, type=21, maxRange=65535.0, resolution=1.0, power=0.45, minDelay=0}
heartRate onSensorChanged values = [54.0], accuracy = 2, sensor = {Sensor name="Heart Rate Sensor", vendor="Motorola", version=1, type=21, maxRange=65535.0, resolution=1.0, power=0.45, minDelay=0}
heartRate onSensorChanged values = [55.0], accuracy = 2, sensor = {Sensor name="Heart Rate Sensor", vendor="Motorola", version=1, type=21, maxRange=65535.0, resolution=1.0, power=0.45, minDelay=0}
heartRate onSensorChanged values = [77.0], accuracy = 1, sensor = {Sensor name="Heart Rate Sensor", vendor="Motorola", version=1, type=21, maxRange=65535.0, resolution=1.0, power=0.45, minDelay=0}

Most of the time I was getting the same 53.0 value which doesn't seem to be my real heart rate. 77 could have been the one.

大多数情况下,我都得到了相同的 53.0 值,这似乎不是我的真实心率。77本来可以是一个。

回答by Uwe Post

I had quite a similar problem on the Moto 360. The sensor always returned 0.0f as a value.

我在 Moto 360 上遇到了类似的问题。传感器总是返回 0.0f 作为值。

Then I waited for two minutes, and suddenly values!=0 came in. It seems that this sensor needs a "warmup" before showing anything. Not really astonishing if you take into account that it measures something happening roughly once a second with the unit "beats per minute". It cannot be reliable before one or two minutes have passed. And each app has its own measurement: It doesn't matter if another heartbeat app is also running (like the Moto Body thing).

然后我等了两分钟,突然values!=0进来了。看来这个sensor在显示任何东西之前需要“预热”。如果您考虑到它以“每分钟节拍数”为单位测量大约每秒发生一次的事情,这并不令人惊讶。在一两分钟过去之前,它是不可靠的。并且每个应用程序都有自己的度量:另一个心跳应用程序是否也在运行并不重要(例如 Moto Body 的东西)。

This also means that you mustcreate a service to listen to the sensor (and a binder to pass the sensor's value to your activity or your phone).

这也意味着您必须创建一个服务来监听传感器(以及一个将传感器的值传递给您的活动或手机的绑定器)。

Have a look at the demo project I shared on github: https://github.com/upost/MyHeartbeat

看看我在github上分享的演示项目:https: //github.com/upost/MyHeartbeat

回答by mangledBadger

So, I came here, with the same problem and the simple solution is to remove the application from the watch using the adb:

所以,我来到这里,遇到同样的问题,简单的解决方案是使用 adb 从手表中删除应用程序:

adb -s localhost:4444 uninstall com.*packagename*

Then simply reinstall it using android studio, eclipse or whatever you used originally.

然后只需使用 android studio、eclipse 或您最初使用的任何东西重新安装它。

Thanks to Alexander K for this solution

感谢 Alexander K 提供的解决方案