Professional Documents
Culture Documents
Lập trình với các sensor
Lập trình với các sensor
getSystemService(Context.SENSOR_SERVICE);
Liệt kê các cảm biến có trong thiết bị:
List<Sensor> deviceSensors =
sensorManager.getSensorList(Sensor.TYPE_ALL);
Bước 2: Gọi tên sensor cần sử dụng:
Sensor tên_sensor = sensorManager.getDefaultSensor(Sensor.Kiểu_sensor);
Các bước lập trình
TYPE_PRESSURE Hardware Measures the ambient air pressure in hPa or mbar. Monitoring air pressure
changes.
TYPE_PROXIMITY Hardware Measures the proximity of an object in cm relative to Phone position during a
the view screen of a device. This sensor is typically used call.
to determine whether a handset is being held up to a
person's ear.
TYPE_RELATIVE_HUMIDITY Hardware Measures the relative ambient humidity in percent (%). Monitoring dewpoint,
absolute, and relative
humidity.
TYPE_ROTATION_VECTOR Software or Measures the orientation of a device by providing the Motion detection and
Hardware three elements of the device's rotation vector. rotation detection.
TYPE_TEMPERATURE Hardware Measures the temperature of the device in degrees Monitoring temperatures.
Celsius (°C). This sensor implementation varies across
devices and this sensor was replaced with the
TYPE_AMBIENT_TEMPERATURE sensor in API Level 14
Ví dụ
Liệt ke các cảm biến có sẵn trong thiết bị.
Ví dụ 2
Đọc giá trị của cảm biến tiệm cận. Thông báo khi có
vật tiến đến gần thiết bị.
Bài tập
Đọc giá trị của cảm biến accelerometers
Có 3 giá trị (x,y,z) – values[0], values[1] values[2]
Hiển thị 3 giá trị vừa đọc lên màn hình
Thông báo: điện thoại đang đứng, đang nằm ngang