从Android Wear获取心率 [英] Get heart rate from Android Wear

查看:159
本文介绍了从Android Wear获取心率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个非常简单的问题,许多人都提出了同样的问题,但是我在获取心率方面仍然存在问题。当然,我已经对该解决方案进行了大量研究。

I know this can be quite simple a question and many people have asked the same question but still I have problem in getting the heart rate. Of course I have done so much research for the solution.

通常,对于使用心率传感器,我们需要获得许可并使用 sensormanager ,并且需要mainActivity中的传感器需要registerListener。

Generally, for using a heart rate sensor, we need get the permission and use sensormanager and need registerListener for the sensor in mainActivity.

有人说此处,我们需要分别声明许可,但到目前为止,我只开发了Android Wear的一部分,

Some have said here for SDK 23 or higher, we need declare the permission separately but till present I develop only the part of Android Wear and there is nothing about the phone.

我也尝试过重新安装并重新运行该应用程序。所有这些方法都不适合我。

And I have also tried reinstall and rerun the application. All these methods don't work for me.

这是我的代码,我只想在手表上显示心率。

Here is my code, all I want is showing the heart rate on the watch.

AWHeartRateSensor\wear\src\main\java\com\example\android\awheartratesensor\MainActivity.java

package com.example.android.awheartratesensor;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

import static com.google.android.gms.wearable.DataMap.TAG;

public class MainActivity extends Activity {

private SensorManager mSensorManager;
private Sensor mHeartSensor;
private TextView mTextView;
WatchViewStub mStub;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        mStub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            mTextView.setText("I am here");
        }
    });

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mHeartSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);


    mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            mTextView.setText("I have the sensor manager");
        }
    });

}
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(mSensorEventListener, mHeartSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(mSensorEventListener);
}
private SensorEventListener mSensorEventListener = new SensorEventListener() {


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        final SensorEvent event1=event;

        mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
                mTextView.setText(Float.toString(event1.values[0]));
            }
        });
    }


    };
}

这是 manifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.awheartratesensor">

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

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-sdk android:minSdkVersion="23"
            android:targetSdkVersion="23"
            />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


推荐答案

有两种方法可以解决此问题。

There are two ways to solve this problem.

快速修复

转到应用程序(或应用程序信息)穿戴式设备中的设置部分,并查看是否已为您的应用检查/启用了人体感应器的权限。即使在清单中包括该权限,默认情况下也始终不选中该应用程序的权限。在设置中启用此权限后,该应用将开始正确使用心率传感器。

Go to the app (or 'App Info') section of settings in your wearable device and see if the permission for body sensor's is checked/enabled for your app. The permission for the app is always unchecked by default, even if you include the permission in your manifest. When you enable the permission in settings, the app will start using the heart rate sensor properly.

正确修复

除了在清单中添加权限外,Android还要求您为身体传感器实现运行时权限。仅需要一次此运行时权限。之后,它将始终按预期运行。

In addition to adding the permission in the manifest, Android requires you to implement a runtime permission for 'Body Sensors'. This runtime permission is only required once. After that it will always work as intended.

这篇关于从Android Wear获取心率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆