Android 识别设备在用户'中的位置;手
我需要识别用户是如何持有设备的。因此,我尝试以这种方式将加速度计和磁强计耦合使用:Android 识别设备在用户'中的位置;手,android,accelerometer,device-orientation,magnetometer,Android,Accelerometer,Device Orientation,Magnetometer,我需要识别用户是如何持有设备的。因此,我尝试以这种方式将加速度计和磁强计耦合使用: public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mGravity = event.values.clone(); Log.d("
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
mGravity = event.values.clone();
Log.d("Sensor","ACCEL");
break;
case Sensor.TYPE_MAGNETIC_FIELD:
Log.d("Sensor","MAGNET");
mMagnetic = event.values.clone();
break;
default:
Log.d("Sensor","default");
}
if(mGravity != null && mMagnetic != null) {
//float[] temp = new float[9];
float[] R = new float[9];
//Load rotation matrix into R
SensorManager.getRotationMatrix(R, null,
mGravity, mMagnetic);
//Return the orientation values
float[] values = new float[3];
SensorManager.getOrientation(R, values);
//Convert to degrees
for (int i=0; i < values.length; i++) {
Double degrees = (values[i] * 180) / Math.PI;
values[i] = degrees.floatValue();
}
}
if (mValuesOrientation[0]!=0 || mValuesOrientation[1]!=0 || mValuesOrientation[2]!=0 ){
logOrientation();
}
}
};
传感器更改时的公共无效(传感器事件){
开关(event.sensor.getType()){
外壳传感器.U型加速计:
mGravity=event.values.clone();
Log.d(“传感器”、“加速度”);
打破
外壳传感器。类型\u磁场:
Log.d(“传感器”、“磁铁”);
mmamagnetic=event.values.clone();
打破
违约:
Log.d(“传感器”、“默认值”);
}
if(mGravity!=null和&mmamagnetic!=null){
//浮动[]温度=新浮动[9];
浮动[]R=新浮动[9];
//将旋转矩阵加载到R中
SensorManager.getRotationMatrix(R,null,
mGravity,mMagnetic);
//返回方向值
浮动[]值=新浮动[3];
SensorManager.getOrientation(R,值);
//换算成度
对于(int i=0;i- 在相同的位置,我将获得相似的值。但价值观是如此不同
我该怎么解决呢?这取决于你想要的动作/姿势有多精细。手机中的传感器有点嘈杂,所以虽然你可能能够辨别出大的移动,但微妙的位置不容易在设备上复制。我需要三个或两个值来识别用户的习惯。因此,在24h之后,我可以看到一些值或范围(考虑误差容限)重复几次。