java 如何从 Android Wear 读取心率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36760344/
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-11-03 01:50:12  来源:igfitidea点击:

How to read Heart rate from Android Wear

javaandroid

提问by Damian Ugalde

I need to read the heart rate of the user at that instant. I tried using this code located in my main activity in the Wear project.

我需要读取用户在那一刻的心率。我尝试使用位于 Wear 项目的主要活动中的此代码。

public class MainActivity extends WearableActivity implements SensorEventListener {

    private static final String TAG = "MainActivity";
    private TextView mTextViewHeart;
    SensorManager mSensorManager;
    Sensor mHeartRateSensor;
    SensorEventListener sensorEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextViewHeart = (TextView) findViewById(R.id.heart);
        mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
        mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
        mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
        Log.i(TAG, "LISTENER REGISTERED.");
        mTextViewHeart.setText("Something here");


        mSensorManager.registerListener(sensorEventListener, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);
    }


    public void onResume(){
        super.onResume();
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.d(TAG, "onAccuracyChanged - accuracy: " + accuracy);
    }

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
            String msg = "" + (int)event.values[0];
            mTextViewHeart.setText(msg);
            Log.d(TAG, msg);
        }
        else
            Log.d(TAG, "Unknown sensor type");
    }

}

This code is simply not working, giving me an error saying:

这段代码根本不起作用,给我一个错误说:

E/SensorManager﹕ sensor or listener is null

It works fine if I use anything else but the heart rate sensor.

如果我使用除心率传感器以外的任何其他东西,它工作正常。

I am using:

我在用:

  • LG G4 with android API level 23
  • LG Urbane watch (Which I know has a heart rate sensor) with Wear API level 23
  • 具有安卓 API 级别 23 的 LG G4
  • 具有 Wear API 级别 23 的 LG Urbane 手表(我知道它有一个心率传感器)

Thank you for your help.

谢谢您的帮助。

采纳答案by Alejandro Castilla

I faced the same problem as you can see in this question. We have the same wear devices, so I'm almost sure that you have missed this on your wear AndroidManifest.xml:

我遇到了与您在此问题中看到的相同的问题。我们有相同的穿戴设备,所以我几乎可以肯定您在穿戴 AndroidManifest.xml 时错过了这个:

<uses-permission android:name="android.permission.BODY_SENSORS" />

Also, your should go to Permissions in Settings in order to check if that permission is enabled.

此外,您应该转到“设置”中的“权限”以检查该权限是否已启用。

You can check my project on GitHub, which is working for now.

您可以在GitHub 上查看我的项目,该项目目前正在运行。

回答by MoGa

Actually you are implementing the SensorEventListener interface and created an instance of SensorEventListener without initializing it.

实际上,您正在实现 SensorEventListener 接口并创建了一个 SensorEventListener 实例,而没有对其进行初始化。

Either initialize the sensor:

要么初始化传感器:

SensorEventListener sensorEventListener = new SensorEventListener(){ ... };

or remove it and use "this":

或删除它并使用“this”:

mSensorManager.registerListener(sensorEventListener, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);

change it to:

将其更改为:

mSensorManager.registerListener(this, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);

回答by Ventis

Don't forget, from SDK 23 onwards you have to ask for permissions separately on the wear device and the phone. The Wear app no longer inherits permissions from the phone app.

不要忘记,从 SDK 23 开始,您必须分别在穿戴设备和手机上请求权限。Wear 应用不再继承手机应用的权限。

https://developer.android.com/training/articles/wear-permissions.html

https://developer.android.com/training/articles/wear-permissions.html

回答by sean le roy

The permissions thing has caught me out a few times now. While I'm yet to implement code to request permission to use the sensors, a simple workaround is to go into settings on the wearable, then select permissions and locate your application. When you click on your application you'll notice sensors is disabled, so just click on it to enable them. Should work then!

权限问题现在已经让我失望了几次。虽然我还没有实现代码来请求使用传感器的权限,但一个简单的解决方法是进入可穿戴设备的设置,然后选择权限并找到您的应用程序。当您单击应用程序时,您会注意到传感器已禁用,因此只需单击它即可启用它们。那么应该工作!