我为穿戴操作系统制作了一个简单的应用程序,可以在文本视图上显示心率。该应用程序运行完美,没有错误,但在显示值时,它只说 1。传感器与其他应用程序配合使用正常。
public class MainActivity extends WearableActivity implements SensorEventListener{
private TextView mTextView;
private TextView mTextViewHeart;
private SensorManager mSensorManager;
private Sensor mHeartRateSensor;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.text);
mTextViewHeart = findViewById(R.id.heart);
// Enables Always-on
setAmbientEnabled();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
assert mSensorManager != null;
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_BEAT);
}
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@SuppressLint("SetTextI18n")
public final void onSensorChanged(SensorEvent event) {
float mHeartRateFloat = event.values[0];
int mHeartRate = Math.round(mHeartRateFloat);
mTextViewHeart.setText(Integer.toString(mHeartRate));
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
这是所需的传感器类型吗?
Sensor.TYPE_HEART_BEAT
您可以考虑将其更改为
m心率传感器 = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE(;