Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Chế độ báo cáo
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Cảm biến có thể tạo sự kiện theo nhiều cách gọi là chế độ báo cáo; mỗi loại cảm biến chỉ có một chế độ báo cáo liên kết với nó.
Có 4 chế độ báo cáo.
Liên tục
Các sự kiện được tạo với tốc độ không đổi do tham số sampling_period_ns
truyền vào hàm batch
xác định. Ví dụ về các cảm biến sử dụng chế độ báo cáo liên tục là gia tốc kế và con quay hồi chuyển.
Khi thay đổi
Sự kiện chỉ được tạo nếu các giá trị đo lường đã thay đổi.
Việc kích hoạt cảm biến ở cấp HAL (gọi activate(..., enable=1)
trên cảm biến đó) cũng kích hoạt một sự kiện, nghĩa là HAL phải trả về một sự kiện ngay lập tức khi cảm biến thay đổi được kích hoạt. Ví dụ về các cảm biến sử dụng chế độ báo cáo khi thay đổi là các loại cảm biến đếm bước, cảm biến khoảng cách và cảm biến nhịp tim.
Tham số sampling_period_ns
được truyền đến hàm batch
dùng để đặt thời gian tối thiểu giữa các sự kiện liên tiếp, nghĩa là không được tạo sự kiện cho đến khi sampling_period_ns
nano giây trôi qua kể từ sự kiện cuối cùng, ngay cả khi giá trị đã thay đổi kể từ đó. Nếu giá trị thay đổi, một sự kiện phải được tạo ngay khi sampling_period_ns
đã trôi qua kể từ sự kiện gần đây nhất.
Ví dụ: giả sử:
- Chúng ta kích hoạt bộ đếm bước bằng
sampling_period_ns = 10 * 10^9
(10 giây).
- Chúng ta đi bộ trong 55 giây, sau đó đứng yên trong một phút.
- Các sự kiện được tạo khoảng 10 giây trong phút đầu tiên (bao gồm cả thời điểm
t=0
do cảm biến được kích hoạt và t=60
giây), tổng cộng có 7 sự kiện. Không có sự kiện nào được tạo trong phút thứ hai vì giá trị số bước không thay đổi sau t=60
giây.
Một lần
Khi phát hiện một sự kiện, cảm biến sẽ tự huỷ kích hoạt rồi gửi một sự kiện thông qua HAL. Thứ tự rất quan trọng để tránh tình trạng tương tranh.
(Bạn phải tắt cảm biến trước khi sự kiện được báo cáo thông qua HAL). Không có sự kiện nào khác được gửi cho đến khi cảm biến được kích hoạt lại.
Chuyển động đáng kể là một ví dụ về loại cảm biến này.
Cảm biến một lần chụp đôi khi được gọi là cảm biến kích hoạt.
Các tham số sampling_period_ns
và max_report_latency_ns
được truyền đến hàm batch
sẽ bị bỏ qua. Không thể lưu trữ các sự kiện từ sự kiện một lần trong FIFO phần cứng; các sự kiện phải được báo cáo ngay khi được tạo.
Đặc biệt
Hãy xem nội dung mô tả từng loại cảm biến để biết thông tin chi tiết về thời điểm tạo sự kiện.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Reporting modes\n\nSensors can generate events in different ways called reporting modes;\neach sensor type has one and only one reporting mode associated with it.\nFour reporting modes exist.\n\nContinuous\n----------\n\nEvents are generated at a constant rate defined by the\n[sampling_period_ns](/docs/core/interaction/sensors/batching#sampling_period_ns)\nparameter passed to the `batch` function. Example sensors\nusing the continuous reporting mode are\n[accelerometers](/docs/core/interaction/sensors/sensor-types#accelerometer)\nand [gyroscopes](/docs/core/interaction/sensors/sensor-types#gyroscope).\n\nOn-change\n---------\n\nEvents are generated only if the measured values have changed.\nActivating the sensor at the HAL level (calling\n`activate(..., enable=1)` on it) also triggers an event,\nmeaning the HAL must return an event immediately when an on-change sensor\nis activated. Example sensors using the on-change reporting mode are the\nstep counter, proximity, and heart rate sensor types.\n\nThe\n[sampling_period_ns](/docs/core/interaction/sensors/batching#sampling_period_ns)\nparameter passed to the `batch` function is used to set the\nminimum time between consecutive events, meaning an event shouldn't be\ngenerated until `sampling_period_ns` nanoseconds elapsed since\nthe last event, even if the value changed since then. If the value changed,\nan event must be generated as soon as `sampling_period_ns` has\nelapsed since the last event.\n\nFor example, suppose:\n\n- We activate the step counter with `sampling_period_ns = 10 * 10^9` (10 seconds).\n- We walk for 55 seconds, then stand still for one minute.\n- The events are generated about every 10 seconds during the first minute (including at time `t=0` because of the activation of the sensor, and `t=60` seconds), for a total of seven events. No event is generated in the second minute because the value of the step count didn't change after `t=60` seconds.\n\nOne-shot\n--------\n\nUpon detection of an event, the sensor deactivates itself and then sends\na single event through the HAL. Order matters to avoid race conditions.\n(The sensor must be deactivated before the event is reported through the\nHAL). No other event is sent until the sensor is reactivated.\n[Significant\nmotion](/docs/core/interaction/sensors/sensor-types#significant_motion) is an example of this kind of sensor.\n\nOne-shot sensors are sometimes referred to as trigger sensors.\n\nThe `sampling_period_ns` and `max_report_latency_ns`\nparameters passed to the `batch` function are ignored. Events\nfrom one-shot events cannot be stored in hardware FIFOs; the events must\nbe reported as soon as they are generated.\n\nSpecial\n-------\n\nSee the individual [sensor type\ndescriptions](/docs/core/interaction/sensors/sensor-types) for details on when the events are generated."]]