我正在尝试获取加速度计数据,但是我无法在需要时获取它。我希望每秒多多少少听20堂课,这并不重要。事实是,我正在接受的方式更多,如可能的最大化。我一直在看文档,但是仍然每秒获得100个实现。这是代码:
public class MainActivity extends WearableActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAcceSensor;
private final static String TAG = "Wear MainActivity";
private TextView mTextView;
private Button myButton;
private ToggleButton activateSensors;
private int num = 1;
public boolean isOn;
private String datapath = "/message_path";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTextView = findViewById(R.id.text);
//send a message from the wear. This one will not have response.
myButton = findViewById(R.id.wrbutton);
activateSensors = findViewById(R.id.toggleButton);
activateSensors.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked) isOn = true;
else isOn = false;
}
});
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = "Hello device " + num;
//Requires a new thread to avoid blocking the UI
new SendThread(datapath, message).start();
num++;
}
});
// Register the local broadcast receiver to receive messages from the listener.
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
MessageReceiver messageReceiver = new MessageReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mAcceSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Enables Always-on
setAmbientEnabled();
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAcceSensor,
50000, 50000);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this, mAcceSensor);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
String msg = Float.toString(sensorEvent.values[0]) +","+ Float.toString(sensorEvent.values[1]) +","+ Float.toString(sensorEvent.values[2])+","+getCurrentTimeStamp();
if (isOn){
new SendThread(datapath, msg).start();
}
}
}
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTime = dateFormat.format(new Date()); // Find todays date
return currentDateTime;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
(Now a block about sending from one device to the other)
}
如您所见,我将侦听器定义为:mSensorManager.registerListener(this,mAcceSensor, 50000,50000);
这应该每隔50毫秒为我提供一次实现,但是我得到了很多。
答案 0 :(得分:0)
我一直在四处看看,自2016年以来,我发现有报道称在WearOS中,您在此处尝试的代码无关紧要:
mSensorManager.registerListener(this,mAcceSensor,50000,50000);
是否使用都没关系
mSensorManager.registerListener(this, mAcceSensor, SensorManager.SENSOR_DELAY_UI);
或 mSensorManager.registerListener(this,mAcceSensor,SensorManager.SENSOR_DELAY_FASTEST);
或我的代码,它始终使用最快的采样率,大约100 Hz。
Galaxy Nexus: Sensor Sampling Rate becomes faster when sampling more Sensors
Android sensor sampling rate won't drow below 60 hz
最后我选择了一种卑鄙的方式:
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
nowDate = new Date();
if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
String msg = Float.toString(sensorEvent.values[0]) +","+ Float.toString(sensorEvent.values[1]) +","+ Float.toString(sensorEvent.values[2]);
if (isOn && (nowDate.getTime() -startDate.getTime()) >= 40){
startDate = nowDate;
String timeStamp = dateFormat.format(nowDate);
msg = msg +","+timeStamp;
new SendThread(datapath, msg).start();
}
}
}
它可以工作,虽然不完美,但是效果很好。好像在条件条件下选择的值无关紧要,我已经尝试了40、50和60,并且都获得了几乎完美的16-17Hz。我也尝试过从SENSOR_DELAY_UI更改为SENSOR_DELAY_FASTEST,我得到相同的结果,始终为16-17Hz,因此这不是选择问题。
我希望这对遇到相同问题的人有所帮助,并且如果有更时尚的方法,我会继续阅读。