据我所知,moto360 没有 TYPE_HEART_RATE类型的传感器,它被称为被动健康传感器。
问题是这个健康传感器没有给我任何数据,而不是我尝试过的所有其他传感器(如重力、加速度计......)
我已经等了超过 5 分钟,但这个传感器只有在我启动应用程序时才会给我数据。
我试过sdk20,sdk21,sdk22,sdk23 ...仍然没有结果我的清单中也有android.permission.BODY_SENSORS
问题:如何让传感器工作,我该怎么办?
package com.x.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.WindowManager;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mHeartSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
}
});
// keep watch screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Toast.makeText(getApplicationContext(), "Hi Oleg", Toast.LENGTH_LONG).show();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mHeartSensor = mSensorManager.getDefaultSensor(65538); //wellness sensor
mSensorManager.registerListener(this, mHeartSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == 65538) {
String msg = "" + (int) event.values[0];
Log.d("Main Activity", msg);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d("Main Activity", "accuracy : " + accuracy + " sensor : " + sensor.getName());
}
@Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this);
}
}
仅输出此“健康”传感器(仅当应用程序启动时):
D/主要活动:准确度:3 传感器:健康无源传感器
D/主要活动:0