星期四, 5月 17, 2012

Sensor (二)

Anroid應用程式學習筆記

Sensor Overview

大部分的Android裝置有感應器讓你監測裝置在移動和位置的變化,許多裝置也有監測周遭環境的感應器,比如溫度、濕度、壓力及亮度,你可以藉由Android感應器框架存取這些裝置與取得感應器的數據。

Android感應器框架提供許多類別與介面幫助你執行廣泛與感應器有關的任務,例如你可以使用感應器框架完成以下任務:
  • 決定在裝置的哪一個感應器是可用的。
  • 決定一個個別的感應器的能力,比如它的最大範圍、廠商、電源要求等。
  • 取得感應器數據。
  • 註冊與註銷事件監聽器。
此主題提供在Android裝置上可用的感應器的概觀,及感應器框架的介紹。


Introduction to Sensors

Android感應器框架讓你存取許多類型的感應器,一些感應器是基於硬體的,一些感應器是基於軟體的。基於硬體的感應器是建在手持裝置或平板裝置的物理元件,這些硬體感應器截油直接測量特定環境屬性提供數據,比如加速、地磁強度或是角度改變。基於軟體的感應器不是物理裝置,雖然他們模擬基於硬體的感應器。基於軟體的感應器從一個或是多個基於硬體的感應器產生數據,且基於軟體的感應器有時也叫做虛擬感應器或是合成感應器。線性加速感應器與重力感應器是基於軟體的感應器的例子。下表總結了Android平台支援的感應器。

少數的Android裝置有美意種類型的感應器。例如,大部分的手持裝置或平板電腦有一個加速度計和磁力儀,但少數的裝置有晴雨表或溫度計。還有一個裝置可以擁有多個同一類型的感應器。例如,一個裝置可以有兩個重力感應器,兩個有不同的範圍。


Table 1. Sensor types supported by the Android platform.
SensorTypeDescriptionCommon Uses
TYPE_ACCELEROMETERHardwareMeasures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.Motion detection (shake, tilt, etc.).
TYPE_AMBIENT_TEMPERATUREHardwareMeasures the ambient room temperature in degrees Celsius (°C). See note below.Monitoring air temperatures.
TYPE_GRAVITYSoftware or HardwareMeasures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z).Motion detection (shake, tilt, etc.).
TYPE_GYROSCOPEHardwareMeasures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).Rotation detection (spin, turn, etc.).
TYPE_LIGHTHardwareMeasures the ambient light level (illumination) in lx.Controlling screen brightness.
TYPE_LINEAR_ACCELERATIONSoftware or HardwareMeasures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.Monitoring acceleration along a single axis.
TYPE_MAGNETIC_FIELDHardwareMeasures the ambient geomagnetic field for all three physical axes (x, y, z) in μT.Creating a compass.
TYPE_ORIENTATIONSoftwareMeasures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method.Determining device position.
TYPE_PRESSUREHardwareMeasures the ambient air pressure in hPa or mbar.Monitoring air pressure changes.
TYPE_PROXIMITYHardwareMeasures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.Phone position during a call.
TYPE_RELATIVE_HUMIDITYHardwareMeasures the relative ambient humidity in percent (%).Monitoring dewpoint, absolute, and relative humidity.
TYPE_ROTATION_VECTORSoftware or HardwareMeasures the orientation of a device by providing the three elements of the device's rotation vector.Motion detection and rotation detection.
TYPE_TEMPERATUREHardwareMeasures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14Monitoring temperatures.
Sensor Framework


你可以使用Android感應器框架存取這些感應器與取得原始數據,感應器框架為android.hardware套件的一部分,包括以下類別與介面:

SensorManager
你可以使用此類別創造一個感應器服務實例,此類別提供許多方法來存取與列表感應器、註冊與註銷感應器事件監聽器、取得方向資訊。此類別也提供許多用來回報感應器準確度、數據回報率、及校準感應器的感應器常數。
Sensor
你可以使用此類別創造一個指定感應器實例,此類別提供多種方法讓你決定一個感應器的能力。
SensorEvent
系統利用此類別產生一個感應器事件物件,用物件來提供有關感應器事件的資訊。一個感應器事件物件包括以下資訊:原始感應器數據、產生事件的感應器的型態、數據的準確度、事件的時間戳。
SensorEventListener
你可以利用此介面產生兩個回呼方法,當感應器數值改變時或是當感應器準確度改變時接收通知的方法。

在使用這些與感應器有關的API的典型應用程式中施行兩個基本任務:

  • 查明感應器與感應器能力
    • 如果你的應用程式有依賴特定感應器類型與能力的功能,在運行時能查明感應器與感應器的能力是很有用的。例如,你可能想要查明你的裝置中所有的感應器且停用這些依賴了裝置中所沒有的感應器的功能。同樣地,你可能想查明所有指定類型的感應器,選擇能在你的應用程式上有最佳執行效果的感應器執行。
  • 監聽感應器事件
    • 監聽感應器事件來取得原始數據的,一個感應器偵測到參數的改變時會發生一個感應器事件,感應器事件提供了四個資訊:觸發事件的感應器名稱、事件的時間戳、事件的準確度、觸發事件的原始數據。

Sensor Avaibability

當感應器的可用性從一個裝置到另一個裝置之間有所變化時,同樣在Android版本間也有所變化。下表總結了各版本可用的感應器。


Table 2. Sensor availability by platform.
SensorAndroid 4.0
(API Level 14)
Android 2.3
(API Level 9)
Android 2.2
(API Level 8)
Android 1.5
(API Level 3)
TYPE_ACCELEROMETERYesYesYesYes
TYPE_AMBIENT_TEMPERATUREYesn/an/an/a
TYPE_GRAVITYYesYesn/an/a
TYPE_GYROSCOPEYesYesn/a1n/a1
TYPE_LIGHTYesYesYesYes
TYPE_LINEAR_ACCELERATIONYesYesn/an/a
TYPE_MAGNETIC_FIELDYesYesYesYes
TYPE_ORIENTATIONYes2Yes2Yes2Yes
TYPE_PRESSUREYesYesn/a1n/a1
TYPE_PROXIMITYYesYesYesYes
TYPE_RELATIVE_HUMIDITYYesn/an/an/a
TYPE_ROTATION_VECTORYesYesn/an/a
TYPE_TEMPERATUREYes2YesYesYes
1 This sensor type was added in Android 1.5 (API Level 3), but it was not available for use until Android 2.3 (API Level 9).
2 This sensor is available, but it has been deprecated.