The Android sensor framework supports a wide variety of sensor types to measure the conditions of the physical environment and read the raw data from apps. Using sensor drivers, your apps can extend this framework and add new sensor devices connected over Peripheral I/O.

Android 的 传感器框架 支持的传感器类型相对广泛,使用该框架可以测量物理环境的状态并从应用中读取原始数据。而通过使用传感器驱动,您的应用可以扩展这个框架,并且可以添加连接到 Peripheral I/O 上的新设备。

The data from these sensors is delivered through the same SensorManager APIs as the built-in Android sensors. Your app can implement a driver to connect a new sensor of a known type, such as an accelerometer, or a sensor type that Android doesn't currently define, such as a blood glucose sensor.

从这些传感器中获取的数据被通过 和 Android 内置传感器相同的 SensorManager APIs 传递。 您的应用可以实现一个驱动,用来连接一个新的已知类型的传感器设备,例如加速度计,或者是当前 Android 未定义的设备类型,比如血糖传感器。

Implementing the driver


The framework polls your driver periodically when listeners are registered for sensor updates. To respond to poll requests for new data, extend the UserSensorDriver class and override the read() method. Each call to read should return a new UserSensorReading containing the current sensor data.

当监听器已被注册接收传感器更新时,framework 会间歇性地拉取您的驱动。为了响应拉取请求以提供新的数据,需要继承 UserSensorDriver 类并覆写 read() 方法。每次调用这个方法时,应当返回包含当前传感器数据的 UserSensorReading 实例。

Note: For more information on the data format expected for known sensor types, see sensor event values.

注意: 想要获取更多关于已知传感器类型的数据格式信息,查阅传感器事件值.

The framework may call read() at a time when the driver is not able to produce a sensor reading. When this occurs, your driver should throw an IOException.

当传感器驱动不能产生数据更新时,framework 也可能会调用 read() 方法,当这种情况出现时,您的驱动应当抛出一个 IOException

UserSensorDriver mDriver = new UserSensorDriver() {
    // Sensor data values
    float x, y, z;

    public UserSensorReading read() {
        try {
            // the sensor hardware...

            // Return a new reading
            return new UserSensorReading(new float[]{x, y, z});
        } (catch Exception e) {
            // Error occurred reading the sensor hardware
            throw new IOException("Unable to read sensor");

If your sensor supports low power or sleep modes, override the setEnabled() method of your driver implementation to activate them. The framework calls this method to indicate that sensors should be ramped up to deliver readings or put to sleep to save power:

如果您的传感器支持低功耗或睡眠模式,重写驱动实现中的 setEnabled() 方法来激活该功能。Framework 调用这个方法来表征这个传感器应当被唤起传递数据或是进入睡眠模式节能:

UserSensorDriver mDriver = new UserSensorDriver() {

    // Called by the framework to toggle low power modes
    public void setEnabled(boolean enabled) {
        if (enabled) {
            // Exit low power mode
        } else {
            // Enter low power mode

Note: Sensors without low power modes can still use this callback to increase or reduce the reporting frequency of the data in order to manage power consumption.

注意: 没有低功耗模式的传感器同样能使用这个回调方法来提高或降低数据的上报频率以便管理节点的能耗。

Describing the sensor


To add a new sensor driver to the Android framework:

在 Android framework 中添加一种新的传感器需要:

  1. Use the UserSensor.Builder to declare the sensor's type.

    使用 UserSensor.Builder 来声明一种传感器类型。

    For most apps, the sensor type should match one of Android's existing known Sensor types.

    对于大部分应用来说,传感器类型应当与 Android 系统中已有的 传感器 类型匹配。

  2. Provide the sensor name and vendor name of your driver.


  3. Apply the range, resolution, update frequency (delay), and power requirements (if available) of your sensor to the builder. These values assist the framework in selecting the best sensor based on the requests received by SensorManager.

    在构建器中提供您的传感器的范围,分辨率,更新频率(延迟),以及能耗需求(如果可用的话)。这些值可以帮助 framework 根据 SensorManager 收到的需求选择最佳的传感器。

  4. Attach your UserSensorDriver implementation with the setDriver() method.

    使用 setDriver() 方法添加您的 UserSensorDriver 实现。

UserSensor accelerometer = UserSensor.builder()

When implementing a sensor for which Android does not have a defined type:

当实现一种 Android 系统未定义的传感器时:

  1. Replace setType() with setCustomType() in the builder.

    在构建器中使用 setCustomType() 替换setType()

  2. Provide a numeric sensor type that is TYPE_DEVICE_PRIVATE_BASE or larger.

    提供一个数值为 TYPE_DEVICE_PRIVATE_BASE 或更大的数字型传感器类型。

  3. Include a unique string name for the sensor type. This name should be unique system-wide, so using reverse domain notation is recommended.


  4. Include the sensor reporting mode.


UserSensor custom = UserSensor.builder()

## Registering the sensor

## 注册传感器

* * *

Connect your new sensor to the framework by registering it with the `UserDriverManager`:

使用 `UserDriverManager` 将新传感器注册到 framework 中:

~~~ java
public class SensorDriverService extends Service {

    UserSensor mAccelerometer;

    public void onCreate() {
        UserDriverManager manager = UserDriverManager.getManager();

        // Create a new driver implementation
        mAccelerometer = ...;
        // Register the new driver with the framework

    public void onDestroy() {
        UserDriverManager manager = UserDriverManager.getManager();
        // Unregister the driver when finished

With the driver properly registered, apps can receive updates from the associated device using the existing Android sensor framework services.

当传感器被正确注册后,应用可以使用 Android 现有的 传感器框架服务 从相关传感器设备处获取更新。

The registration process can take some time before the sensor is available to clients. Apps interested in a user sensor should register a DynamicSensorCallback to be notified when it is available before registering a listener for sensor readings:

在传感器可用之前,注册过程可能会需要一些时间。在注册传感器数据监听器之前,对用户传感器敏感的应用应当注册一个 DynamicSensorCallback ,这样,当传感器可用时,它将会告知应用:

public class SensorActivity extends Activity implements SensorEventListener {

    private SensorManager mSensorManager;
    private SensorCallback mCallback = new SensorCallback();

    protected void onCreate(Bundle savedInstanceState) {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    protected void onDestroy() {

    public void onSensorChanged(SensorEvent event) {

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    // Listen for registration events from the sensor driver
    private class SensorCallback extends SensorManager.DynamicSensorCallback {
        public void onDynamicSensorConnected(Sensor sensor) {
            // Begin listening for sensor readings
            mSensorManager.registerListener(SensorActivity.this, sensor,

        public void onDynamicSensorDisconnected(Sensor sensor) {
            // Stop receiving sensor readings

Adding the required permission


Add the required permission for the user driver to your app's manifest file:


<uses-permission android:name="" />

results matching ""

    No results matching ""