全栈工程师开发手册 (作者:栾鹏)
安卓教程全解
安卓传感器全解:注册、注销传感器、监听传感器、距离传感器、方向传感器、陀螺仪、加速计、磁场、气压传感器。
1、自定义传感器监听器
//自定义传感器监视器的基本代码
private float[] sensorValues;
final SensorEventListener mySensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
//监视传感器变化
Sensor sensor=sensorEvent.sensor; //触发该事件的传感器对象
int accuracy = sensorEvent.accuracy; //当事件发生时传感器的精确度(low、medium、high、unreliable)
String accuracystr="";
switch (accuracy) {
case SensorManager.SENSOR_STATUS_ACCURACY_LOW:accuracystr="精确度低需要校准";break;
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:accuracystr="平均精确度";break;
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:accuracystr="最高精确度";break;
case SensorManager.SENSOR_STATUS_UNRELIABLE:accuracystr="数据不可靠";break;
default:break;
}
long timestamp = sensorEvent.timestamp; //传感器事件发生的时间(以纳秒为单位)
sensorValues = sensorEvent.values; //包含了已检测到的新值的浮点型数组,每个传感器各不相同,加速度传感器监听器为x、y、z轴变化量
StringBuffer sb = new StringBuffer();
for(int i = 0; i < sensorValues.length; i++)
sb. append(String.valueOf(sensorValues[i])+",");
Log.v("传感器监听", "触发传感器:"+sensor.getName());
Log.v("传感器监听", "传感器精度:"+accuracystr);
Log.v("传感器监听", "传感器时间戳:"+timestamp);
Log.v("传感器监听", "传感器新值:"+sb.toString());
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//对传感器精度的改变做出响应
}
};
2、注册和注销一个传感器事件监听器
private void registerSensor()
{
SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); //获取传感器系统服务
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); //这里使用距离传感器
// 1 加速度传感器 TYPE_ACCELEROMETER
// 2 温度传感器 TYPE_AMBIENT_TEMPERATURE
// 3 陀螺仪传感器 TYPE_GYROSCOPE
// 4 光线传感器 TYPE_LIGHT
// 5 磁场传感器 TYPE_MAGNETIC_FIELD
// 6 气压传感器 TYPE_PRESSURE
// 7 临近传感器 TYPE_PROXIMITY
// 8 湿度传感器 TYPE_RELATIVE_HUMIDITY
// 9 方向传感器 TYPE_ORIENTATION
// 10 重力传感器 TYPE_GRAVITY
// 11 线性加速传感器 TYPE_LINEAR_ACCELERATION
// 12 旋转向量传感器 TYPE_ROTATION_VECTOR
//注册监听事件
sensorManager.registerListener(mySensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL); //监听器、传感器、监听频率
//SENSOR_DELAY_NORMAL //默认的更新速率,延时:200ms
//SENSOR_DELAY_FASTEST //可以实现的最快更新速率,延时:0ms
//SENSOR_DELAY_GAME //适合控制游戏的更新速率,延时:20ms
//SENSOR_DELAY_UI //适合更新UI的速率,延时:60ms
//注销
//sensorManager.unregisterListener(mySensorEventListener);
}
不建议使用这种方式,方向传感器正在被废弃。
//废弃的方向传感器
private void OrientationFromOrientation()
{
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(myOrientationListener,sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
}
//方向传感器监听器
final SensorEventListener myOrientationListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float headingAngle = sensorEvent.values[0];
float pitchAngle = sensorEvent.values[1];
float rollAngle = sensorEvent.values[2];
Log.v("方向传感器计算的方向", "方位角:"+headingAngle);
Log.v("方向传感器计算的方向", "俯仰角:"+pitchAngle);
Log.v("方向传感器计算的方向", "横滚角:"+rollAngle);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
//使用陀螺仪计算方向变化
private void OrientationFromGyro()
{
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(myGyroListener,sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_NORMAL);
}
//使用陀螺仪计算方向变化
final float nanosecondsPerSecond = 1.0f / 1000000000.0f;
private long lastTime = 0;
final float[] angle = new float[3];
SensorEventListener myGyroListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
if (lastTime != 0) {
final float dT = (sensorEvent.timestamp - lastTime) *nanosecondsPerSecond;
angle[0] += sensorEvent.values[0] * dT;
angle[1] += sensorEvent.values[1] * dT;
angle[2] += sensorEvent.values[2] * dT;
Log.v("陀螺仪计算的方向", "方位角:"+angle[0]);
Log.v("陀螺仪计算的方向", "俯仰角:"+angle[1]);
Log.v("陀螺仪计算的方向", "横滚角:"+angle[2]);
}
lastTime = sensorEvent.timestamp;
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
需要同时注册加速度传感器和磁场传感器,通过传感器值,计算方向
private void OrientationFromAccelerometer()
{
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(AccelerometerAndMagnetic,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL); //注册加速度
sm.registerListener(AccelerometerAndMagnetic,sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL); //注册磁场
}
//加速度和磁场传感器监听器
final SensorEventListener AccelerometerAndMagnetic = new SensorEventListener()
{
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelerometerValues=sensorEvent.values; //获取加速度传感器数据
}
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magneticFieldValues=sensorEvent.values; //获取磁场传感器数据
}
Accelerome2Orientation(); //根据加速度和磁场数据,计算方向
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
private float[] accelerometerValues; //加速度传感器的取值
private float[] magneticFieldValues; //磁场传感器的取值
private void Accelerome2Orientation()
{
if (accelerometerValues==null || magneticFieldValues==null) {
return;
}
float[] values = new float[3];
float[] R = new float[9];
SensorManager.getRotationMatrix(R, null,accelerometerValues,magneticFieldValues);
SensorManager.getOrientation(R, values);
// 转化成方向的取值
values[0] = (float) Math.toDegrees(values[0]); // Azimuth,方位角,当设备朝向北方时,为0
values[1] = (float) Math.toDegrees(values[1]); // Pitch,俯仰角,绕x轴的旋转
values[2] = (float) Math.toDegrees(values[2]); // Roll,横滚角,绕y轴的旋转
Log.v("加速计和磁场计算的方向", "方位角:"+values[0]);
Log.v("加速计和磁场计算的方向", "俯仰角:"+values[1]);
Log.v("加速计和磁场计算的方向", "横滚角:"+values[2]);
}
private void AltitudeFromPressure()
{
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(myPressureListener,sm.getDefaultSensor(Sensor.TYPE_PRESSURE),SensorManager.SENSOR_DELAY_NORMAL);
}
//使用气压计传感器确定当前海拔
final SensorEventListener myPressureListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_PRESSURE) {
float currentPressure = sensorEvent.values[0];
//计算海拔
float altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, currentPressure);
Log.v("气压计计算的海拔", "海拔:"+altitude);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};