简体   繁体   English

"如何从 Android Wear 读取心率"

[英]How to read Heart rate from Android Wear

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具有 android API 级别 23 的 LG G4
  • LG Urbane watch (Which I know has a heart rate sensor) with Wear API level 23带有 Wear API 级别 23 的 LG Urbane 手表(我知道它有一个心率传感器)

Thank you for your help.谢谢您的帮助。

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上查看我的项目,该项目目前正在运行。

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);

Don't forget, from SDK 23 onwards you have to ask for permissions separately on the wear device and the phone. 不要忘记,从SDK 23开始,您必须分别在佩戴设备和手机上申请权限。 The Wear app no longer inherits permissions from the phone app. Wear应用程序不再继承手机应用程序的权限。

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

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! 应该工作呢!

非常感谢,它也对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM