Code mẫu đọc tất cả cảm biến - Sensor trên thiết bị Android
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
Log.d(TAG, "onCreate: Total Sensor: " + sensors.size());
for (Sensor s : sensors) {
Log.d(TAG, "onCreate: " + s);
}
- Dòng 1: lấy về đối tượng
SensorManager
bởi phương thứcgetSystemService()
- Dòng 2: lấy về tất cả Sensor trên thiết bị Android.
- Dòng 3: xuất ra tổng số Sensor có trên thiết bị.
- Dòng 4, 5, 6: duyệt qua từng Sensor có trong danh sách và xuất ra thông tin chi tiết của từng Sensor.
Kết quả xuất ra sau khi chạy đoạn mã trên được thử nghiệm trên thiết bị Sky Pantech A900 là:
onCreate: Total Sensor: 26
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="BMA2X2 Accelerometer/Temperature/Double-tap", vendor="BOSCH", version=1, type=1, maxRange=39.240005, resolution=0.07661438, power=0.13, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="MS-3R Magnetometer", vendor="Yamaha", version=34865414, type=2, maxRange=0.0, resolution=0.30059814, power=0.4, minDelay=16666}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="MS-3R Magnetometer Uncalibrated", vendor="Yamaha", version=34865414, type=14, maxRange=0.0, resolution=0.30059814, power=0.4, minDelay=16666}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="BMG160 Gyroscope", vendor="BOSCH", version=1, type=4, maxRange=500.0, resolution=0.0, power=5.0, minDelay=5000}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="BMG160 Gyroscope Uncalibrated", vendor="BOSCH", version=1, type=16, maxRange=500.0, resolution=0.0, power=5.0, minDelay=5000}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="apds9930 avago_dev.PRX", vendor="AVAGO", version=1, type=8, maxRange=5.000305, resolution=0.10070801, power=12.675, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="apds9930 avago_dev.LIG", vendor="AVAGO", version=1, type=5, maxRange=2000.0, resolution=0.009994507, power=0.175, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Gravity", vendor="Qualcomm", version=1, type=9, maxRange=39.240005, resolution=0.07661438, power=5.13, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Linear Acceleration", vendor="Qualcomm", version=1, type=10, maxRange=39.240005, resolution=0.07661438, power=5.13, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Rotation Vector", vendor="Qualcomm", version=1, type=11, maxRange=1.0, resolution=5.9604645E-8, power=5.53, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Step Detector", vendor="Qualcomm", version=1, type=18, maxRange=1.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Step Counter", vendor="Qualcomm", version=1, type=19, maxRange=1.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Significant Motion Detector", vendor="Qualcomm", version=1, type=17, maxRange=1.0, resolution=1.0, power=0.13, minDelay=-1}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Game Rotation Vector", vendor="Qualcomm", version=1, type=15, maxRange=1.0, resolution=5.9604645E-8, power=5.13, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Geomagnetic Rotation Vector", vendor="Qualcomm", version=1, type=20, maxRange=1.0, resolution=5.9604645E-8, power=0.53, minDelay=16666}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Orientation", vendor="Qualcomm", version=1, type=3, maxRange=360.0, resolution=0.1, power=5.53, minDelay=8333}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="AMD", vendor="Qualcomm", version=1, type=33171006, maxRange=1.0, resolution=1.0, power=1.0, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="RMD", vendor="Qualcomm", version=1, type=33171007, maxRange=1.0, resolution=1.0, power=1.0, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="VMD", vendor="Qualcomm", version=1, type=33171008, maxRange=1.0, resolution=1.0, power=1.0, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Basic Gestures", vendor="Qualcomm", version=1, type=33171000, maxRange=7.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Facing", vendor="Qualcomm", version=1, type=33171002, maxRange=3.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Tilt", vendor="Qualcomm", version=1, type=33171003, maxRange=180.0, resolution=0.1, power=5.0, minDelay=10000}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Gyro Tap", vendor="Qualcomm", version=1, type=33171001, maxRange=6.0, resolution=1.0, power=5.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Pedometer", vendor="Qualcomm", version=1, type=33171009, maxRange=1.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="PEDESTRIAN-ACTIVITY-MONITOR", vendor="Qualcomm", version=1, type=33171010, maxRange=65535.0, resolution=1.0, power=0.13, minDelay=0}
06-11 10:04:15.868 32761-32761/com.example.nguyennghia.orientationapp D/MainActivity: onCreate: {Sensor name="Motion Accel", vendor="Qualcomm", version=1, type=33171011, maxRange=39.240005, resolution=0.07661438, power=0.13, minDelay=0}