ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
เซ็นเซอร์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เซ็นเซอร์ Android ให้สิทธิ์แอปพลิเคชันเข้าถึงเซ็นเซอร์ทางกายภาพที่อยู่เบื้องหลังของอุปกรณ์เคลื่อนที่ โดยเป็นอุปกรณ์เสมือนที่ให้ข้อมูลซึ่งกำหนดโดย sensors.h ซึ่งเป็นเลเยอร์การแยกแยะฮาร์ดแวร์ (HAL) ของเซ็นเซอร์
เซ็นเซอร์ Android คืออะไร
เซ็นเซอร์ Android เป็นอุปกรณ์เสมือนจริงที่ให้ข้อมูลจากชุดเซ็นเซอร์ทางกายภาพ ได้แก่ ตัวตรวจวัดความเร่ง เซ็นเซอร์การหมุน เซ็นเซอร์วัดค่าความเข้มข้นของสนามแม่เหล็ก บารอมิเตอร์ ความชื้น ความดัน แสง เซ็นเซอร์ตรวจหาบุคคลในบริเวณใกล้เคียง และเซ็นเซอร์อัตราการเต้นของหัวใจ
กล้อง เซ็นเซอร์ลายนิ้วมือ ไมโครโฟน และหน้าจอสัมผัสจะไม่รวมอยู่ในรายการอุปกรณ์จริงที่ให้ข้อมูล อุปกรณ์เหล่านี้มีกลไกการรายงานของตนเอง การแยกเป็นค่ากำหนด แต่โดยทั่วไปเซ็นเซอร์ Android จะแสดงข้อมูลแบนด์วิดท์ที่ต่ำลง เช่น "100 Hz x 3 ช่อง" สำหรับเครื่องวัดความเร่งเทียบกับ "25 Hz x 8 MP x 3 ช่อง" สำหรับกล้อง หรือ "44 kHz x 1 ช่อง" สำหรับไมโครโฟน
Android ไม่ได้กำหนดวิธีเชื่อมต่อเซ็นเซอร์ต่างๆ กับระบบวงจรรวมบนชิป (SoC)
- ชิปเซ็นเซอร์มักเชื่อมต่อกับ SoC ผ่านฮับเซ็นเซอร์ ซึ่งช่วยให้สามารถตรวจสอบและประมวลผลข้อมูลได้โดยใช้พลังงานต่ำ
- มักใช้ Inter-Integrated Circuit (I2C) หรือ Serial Peripheral Interface (SPI) เป็นกลไกการรับส่ง
- สถาปัตยกรรมบางอย่างเป็นแบบลําดับชั้นเพื่อลดการใช้พลังงาน โดยมีการประมวลผลขั้นต่ำในวงจรรวมเฉพาะแอปพลิเคชัน (ASIC เช่น การตรวจจับการเคลื่อนไหวในชิปเซ็นเซอร์ความเร่ง) และประมวลผลเพิ่มเติมในไมโครคอนโทรลเลอร์ (เช่น การตรวจจับการก้าวในฮับเซ็นเซอร์)
- ผู้ผลิตอุปกรณ์จะเลือกสถาปัตยกรรมตามลักษณะความแม่นยำ กำลัง ราคา และขนาดแพ็กเกจ ดูข้อมูลเพิ่มเติมได้ในสแต็กเซ็นเซอร์
- ความสามารถในการจัดกลุ่มเป็นข้อควรพิจารณาที่สําคัญในการเพิ่มประสิทธิภาพ ดูข้อมูลเพิ่มเติมที่การรวม
เซ็นเซอร์ Android แต่ละตัวมี "ประเภท" ที่แสดงลักษณะการทำงานของเซ็นเซอร์และข้อมูลที่ให้
- ประเภทเซ็นเซอร์อย่างเป็นทางการของ Android จะกำหนดไว้ใน sensors.h โดยใช้ชื่อ SENSOR_TYPE_…
- เซ็นเซอร์ส่วนใหญ่มีประเภทเซ็นเซอร์อย่างเป็นทางการ
- ประเภทดังกล่าวมีอยู่ในเอกสารประกอบของ Android SDK
- ลักษณะการทำงานของเซ็นเซอร์ประเภทดังกล่าวได้รับการทดสอบในชุดเครื่องมือทดสอบความเข้ากันได้ (CTS) ของ Android
- หากผู้ผลิตผสานรวมเซ็นเซอร์ประเภทใหม่ในอุปกรณ์ Android ผู้ผลิตสามารถกำหนดประเภทชั่วคราวของตนเองเพื่ออ้างอิงถึงเซ็นเซอร์ได้
- ประเภทดังกล่าวไม่มีเอกสารประกอบ นักพัฒนาแอปพลิเคชันจึงมีแนวโน้มที่จะไม่ใช้ เนื่องจากไม่ทราบเกี่ยวกับประเภทดังกล่าว หรือทราบว่ามีการใช้งานประเภทดังกล่าวน้อยมาก (มีเฉพาะในอุปกรณ์บางรุ่นจากผู้ผลิตรายนี้เท่านั้น)
- แต่ CTS ไม่ได้ทดสอบ
- เมื่อ Android กำหนดประเภทเซ็นเซอร์อย่างเป็นทางการสำหรับเซ็นเซอร์ประเภทนี้แล้ว ผู้ผลิตจะต้องหยุดใช้ประเภทชั่วคราวของตนเองและใช้ประเภทอย่างเป็นทางการแทน วิธีนี้จะช่วยให้นักพัฒนาแอปพลิเคชันจำนวนมากขึ้นใช้เซ็นเซอร์ได้
- รายการเซ็นเซอร์ทั้งหมดที่มีอยู่ในอุปกรณ์จะได้รับการรายงานโดยการติดตั้งใช้งาน HAL
- เซ็นเซอร์ประเภทเดียวกันอาจมีหลายตัว เช่น เซ็นเซอร์ตรวจหาบุคคลในบริเวณใกล้เคียง 2 ตัวหรือตัวตรวจวัดความเร่ง 2 ตัว
- แอปพลิเคชันส่วนใหญ่จะขอเซ็นเซอร์เพียงตัวเดียวในประเภทหนึ่งๆ ตัวอย่างเช่น แอปพลิเคชันที่ขอเครื่องวัดความเร่งเริ่มต้นจะได้รับเครื่องวัดความเร่งรายการแรกในรายการ
- เซ็นเซอร์มักกำหนดโดยคู่เซ็นเซอร์ที่ตื่นขึ้นและเซ็นเซอร์ที่ไม่ตื่นขึ้น โดยเซ็นเซอร์ทั้ง 2 ประเภทเป็นเซ็นเซอร์ประเภทเดียวกัน แต่แตกต่างกันตรงลักษณะการตื่นขึ้น
เซ็นเซอร์ Android ให้ข้อมูลเป็นชุดเหตุการณ์ของเซ็นเซอร์
เหตุการณ์แต่ละรายการประกอบด้วยข้อมูลต่อไปนี้
การตีความข้อมูลที่รายงานจะขึ้นอยู่กับประเภทเซ็นเซอร์ ดูรายละเอียดเกี่ยวกับข้อมูลที่รายงานสำหรับเซ็นเซอร์แต่ละประเภทได้จากคำจำกัดความประเภทเซ็นเซอร์
เอกสารประกอบที่มีอยู่
มีกลุ่มเป้าหมายเป็นนักพัฒนาแอป
- ภาพรวม
- ข้อมูลอ้างอิง SDK
- Stack Overflow และเว็บไซต์บทแนะนำ
- เนื่องจากบางครั้งเอกสารประกอบเกี่ยวกับเซ็นเซอร์ขาดหายไป นักพัฒนาซอฟต์แวร์จึงต้องหันไปหาคำตอบในเว็บไซต์ถามและตอบอย่าง Stack Overflow
- นอกจากนี้ยังมีเว็บไซต์บทแนะนำบางแห่ง แต่ไม่ได้ครอบคลุมฟีเจอร์ล่าสุด เช่น การแยกกลุ่ม การเคลื่อนไหวที่สำคัญ และเวกเตอร์การหมุนของเกม
- คำตอบในนั้นอาจไม่ถูกต้องเสมอไป และแสดงจุดที่ต้องให้เอกสารเพิ่มเติม
มีกลุ่มเป้าหมายเป็นผู้ผลิต
- ภาพรวม
- เลเยอร์การจัดการฮาร์ดแวร์โดยตรง (HAL)
- CDD (เอกสารคำจำกัดความความเข้ากันได้) ของ Android
- /compatibility/android-cdd.pdf
- ดูส่วนที่เกี่ยวข้องกับเซ็นเซอร์
- CDD มีข้อกำหนดที่ไม่เข้มงวดมากนัก การปฏิบัติตามข้อกำหนดของ CDD จึงยังไม่เพียงพอที่จะรับประกันเซ็นเซอร์คุณภาพสูง
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Sensors\n\nAndroid sensors give applications access to a mobile device's underlying\nphysical sensors. They are data-providing virtual devices defined by [sensors.h](https://android.googlesource.com/platform/hardware/libhardware/+/android16-release/include/hardware/sensors.h), the sensor Hardware Abstraction Layer (HAL).\n\nWhat are Android sensors?\n-------------------------\n\nAndroid sensors are virtual devices that provide data coming from a set of\nphysical sensors: accelerometers, gyroscopes, magnetometers, barometer, humidity,\npressure, light, proximity and heart rate sensors.\n\nNot included in the list of physical devices providing data are camera,\nfingerprint sensor, microphone, and touch screen. These devices have their own\nreporting mechanism; the separation is arbitrary, but in general, Android sensors\nprovide lower bandwidth data. For example, \"100hz x 3 channels\" for an\naccelerometer versus \"25hz x 8 MP x 3 channels\" for a camera or \"44kHz x 1\nchannel\" for a microphone.\n\nAndroid does not define how the different physical sensors are connected\nto the system on chip (SoC).\n\n- Often, sensor chips are connected to the SoC through a [sensor hub](/docs/core/interaction/sensors/sensor-stack#sensor_hub), allowing some low-power monitoring and processing of the data.\n- Often, Inter-Integrated Circuit (I2C) or Serial Peripheral Interface (SPI) is used as the transport mechanism.\n- To reduce power consumption, some architectures are hierarchical, with some minimal processing being done in the application-specific integrated circuit (ASIC - like motion detection on the accelerometer chip), and more is done in a microcontroller (like step detection in a sensor hub).\n- It is up to the device manufacturer to choose an architecture based on accuracy, power, price and package-size characteristics. See [Sensor stack](/docs/core/interaction/sensors/sensor-stack) for more information.\n- Batching capabilities are an important consideration for power optimization. See [Batching](/docs/core/interaction/sensors/batching) for more information.\n\nEach Android sensor has a \"type\" representing how the sensor behaves and\nwhat data it provides.\n\n- The official Android [Sensor\n types](/docs/core/interaction/sensors/sensor-types) are defined in [sensors.h](https://android.googlesource.com/platform/hardware/libhardware/+/android16-release/include/hardware/sensors.h) under the names SENSOR_TYPE_...\n - The vast majority of sensors have an official sensor type.\n - Those types are documented in the Android SDK.\n - Behavior of sensors with those types are tested in the Android Compatibility Test Suite (CTS).\n- If a manufacturer integrates a new kind of sensor on an Android device, the manufacturer can define its own temporary type to refer to it.\n - Those types are undocumented, so application developers are unlikely to use them, either because they don't know about them, or know that they are rarely present (only on some devices from this specific manufacturer).\n - They are not tested by CTS.\n - Once Android defines an official sensor type for this kind of sensor, manufacturers must stop using their own temporary type and use the official type instead. This way, the sensor will be used by more application developers.\n- The list of all sensors present on the device is reported by the HAL implementation.\n - There can be several sensors of the same type. For example, two proximity sensors or two accelerometers.\n - The vast majority of applications request only a single sensor of a given type. For example, an application requesting the default accelerometer will get the first accelerometer in the list.\n - Sensors are often defined by [wake-up](/docs/core/interaction/sensors/suspend-mode#wake-up_sensors) and [non-wake-up](/docs/core/interaction/sensors/suspend-mode#non-wake-up_sensors) pairs, both sensors sharing the same type, but differing by their wake-up characteristic.\n\nAndroid sensors provide data as a series of sensor events.\n\nEach [event](/docs/core/interaction/sensors/hal-interface#sensors_event_t)\ncontains:\n\n- a handle to the sensor that generated it\n- the timestamp at which the event was detected or measured, based on [`SystemClock.elapsedRealtimeNanos()`](https://developer.android.com/reference/android/os/SystemClock#elapsedRealtimeNanos())\n- and some data\n\nThe interpretation of the reported data depends on the sensor type. See\nthe [sensor type](/docs/core/interaction/sensors/sensor-types)\ndefinitions for details on what data is reported for each sensor type.\n\nExisting documentation\n----------------------\n\n### Targeted at developers\n\n- Overview\n - \u003chttps://developer.android.com/guide/topics/sensors/sensors_overview.html\u003e\n- SDK reference\n - \u003chttps://developer.android.com/reference/android/hardware/SensorManager\u003e\n - \u003chttps://developer.android.com/reference/android/hardware/SensorEventListener\u003e\n - \u003chttps://developer.android.com/reference/android/hardware/SensorEvent\u003e\n - \u003chttps://developer.android.com/reference/android/hardware/Sensor\u003e\n- Stack Overflow and tutorial websites\n - Because sensors documentation was sometimes lacking, developers resorted to Q\\&A websites like Stack Overflow to find answers.\n - Some tutorial websites exist as well, but do not cover the latest features like batching, significant motion and game rotation vectors.\n - The answers over there are not always right, and show where more documentation is needed.\n\n### Targeted at manufacturers\n\n- Overview\n - This [Sensors](/docs/core/interaction/sensors) page and its sub-pages.\n- Hardware abstraction layer (HAL)\n - [/platform/hardware/libhardware/+/android16-release/include/hardware/sensors.h](https://android.googlesource.com/platform/hardware/libhardware/+/android16-release/include/hardware/sensors.h)\n - Also known as \"sensors.h\"\n - The source of truth. First document to be updated when new features are developed.\n- Android CDD (Compatibility Definition Document)\n - [/compatibility/android-cdd.pdf](/static/docs/compatibility/android-cdd.pdf)\n - See sections relative to sensors.\n - The CDD is lenient, so satisfying the CDD requirements is not enough to ensure high quality sensors."]]