Android Sensor的简单调用

本文介绍了Android平台中如何使用SensorManager类获取所有可用的传感器,并通过具体示例展示了不同类型的传感器如加速度传感器、陀螺仪传感器等的获取方法及其基本信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
        for (Sensor s : allSensors) {
            String tempString = "\n" + "  设备名称:" + s.getName() + "\n" + "  设备版本:" + s.getVersion() + "\n" + "  供应商:" + s.getVendor() + "\n";
            switch (s.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    Log.e("--------",s.getType() + " 加速度传感器" + tempString);
                    break;
                case Sensor.TYPE_GYROSCOPE:
                    Log.e("--------",s.getType() + " 陀螺仪传感器" + tempString);
                    break;
                case Sensor.TYPE_LIGHT:
                    Log.e("--------",s.getType() + " 环境光线传感器" + tempString);
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    Log.e("--------",s.getType() + " 电磁场传感器" + tempString);
                    break;
                case Sensor.TYPE_ORIENTATION:
                    Log.e("--------",s.getType() + " 方向传感器" + tempString);
                    break;
                case Sensor.TYPE_PRESSURE:
                    Log.e("--------",s.getType() + " 压力传感器" + tempString);
                    break;
                case Sensor.TYPE_PROXIMITY:
                    Log.e("--------",s.getType() + " 距离传感器" + tempString);
                    break;
                case Sensor.TYPE_TEMPERATURE:
                    Log.e("--------",s.getType() + " 温度传感器" + tempString);
                    break;
                default:
                    Log.e("--------",s.getType() + " 未知传感器" + tempString);
                    break;
            }
        }
这里是调用各种传感器的方法,简单的记录一下。

public static final int TYPE_ALL = -1;
public static final int TYPE_ACCELEROMETER = 1;//加速度传感器
public static final int TYPE_MAGNETIC_FIELD = 2;//磁场传感器
public static final int TYPE_ORIENTATION = 3;//方向传感器
public static final int TYPE_GYROSCOPE = 4;//陀螺仪传感器
public static final int TYPE_LIGHT = 5;//光传感器
public static final int TYPE_PRESSURE = 6;//压力传感器
public static final int TYPE_TEMPERATURE = 7;//温度传感器
public static final int TYPE_PROXIMITY = 8;//距离传感器
public static final int TYPE_GRAVITY = 9;//重力传感器
public static final int TYPE_LINEAR_ACCELERATION = 10;//线性加速传感器
public static final int TYPE_ROTATION_VECTOR = 11;//旋转矢量传感器
public static final int TYPE_RELATIVE_HUMIDITY = 12;//相对湿度传感器
public static final int TYPE_AMBIENT_TEMPERATURE = 13;//温度传感器
public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14;//磁场未校准传感器
public static final int TYPE_GAME_ROTATION_VECTOR = 15;//游戏矢量旋转传感器
public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16;//陀螺仪未校准传感器
public static final int TYPE_SIGNIFICANT_MOTION = 17;//重要运动传感器
public static final int TYPE_STEP_DETECTOR = 18;//步进探测器传感器
public static final int TYPE_STEP_COUNTER = 19;//步数传感器
public static final int TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20;//地磁旋转矢量传感器
public static final int TYPE_HEART_RATE = 21;//心跳速度传感器
public static final int TYPE_POSE_6DOF = 28;//姿势传感器
public static final int TYPE_STATIONARY_DETECT = 29;//驻留检测传感器
public static final int TYPE_MOTION_DETECT = 30;//运动检测传感器
public static final int TYPE_HEART_BEAT = 31;//心跳传感器
public static final int TYPE_LOW_LATENCY_OFFBODY_DETECT = 34;//低潜伏离体传感器
public static final int TYPE_ACCELEROMETER_UNCALIBRATED = 35;//加速度计未校准传感器
public static final int TYPE_DEVICE_PRIVATE_BASE = 65536;//设备私有基础

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值