问题
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.
Generally, for using a heart rate sensor, we need get the permission and use sensormanager and need registerListener for the sensor in mainActivity.
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]));
}
});
}
};
}
Here is the 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>
回答1:
There are two ways to solve this problem.
For a quick fix
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.
Proper fix
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.
来源:https://stackoverflow.com/questions/44337896/get-heart-rate-from-android-wear