Summary of the sensor

Based on the Android devices have built-in sensors for measuring movement, direction, and various environmental conditions.These sensors can provide the raw data and ensure its accuracy and high accuracy.For example, a weather application may use the equipment of temperature sensor and humidity sensor to calculate and report environmental status.Also, a healthy application will pass step gauge sensor and the heart rate sensor consume more calories and heart rate of the current values. The IWOP platform support sensor is divided into three categories:

 
Motion sensor:
 
The sensors used to measure the current body or arm movement.This kind of sensor including a motion sensor, signal sensor and step gauge sensor.
 
Health sensors:
 
These sensors used to measure the health of the body, currently only supports the heart rate sensor.
 
Environmental sensors:
 
The sensors used to measure various environmental parameters, such as air temperature, pressure and humidity, illumination.This category includes barometer, hygrometer, thermometer.

You can access to all of the available sensors on the device, using the IWOP service to obtain the original data sensor.Sensor service provides classes and interfaces, help you to perform a variety of sensors related tasks.For example, you can use the sensor service does the following:

  • Sure there is some sensors in equipment.
  • To monitor the change of the sensor.

The sensor is introduced

The sensor support list

IWOP sensing services currently supports the following sensors:

 
TYPE_HEART_RATE:
 
Heart rate sensor, measuring the heart rate.
 
TYPE_GESTURE:
 
Signal sensor, measure the current state of the hand (hand/wave, etc.).
 
TYPE_MOTION:
 
Motion sensors, measuring the body motion (run/ride, etc.).
 
TYPE_STEP_COUNTER:
 
Step gauge sensor, the measuring steps.
 
TYPE_RELATIVE_HUMIDITY:
 
Environmental humidity sensor, humidity measurement environment.
 
TYPE_AMBIENT_TEMPERATURE:
 
Ambient temperature sensor, temperature measurement.
 
TYPE_PRESSURE:
 
Air pressure sensor, measurement environment atmosphere.

Sensing services framework

More than you can visit the sensor by IWOP sensor service framework, service in com. Ingenic. Iwds. Smartsense package, which contains the following classes and interfaces:

 
SensorServiceManager:
 
You can use this class to obtain instances of different kinds of sensors.This class provides access to the sensor, the method of registration and cancellation of sensor event listeners.This class also provides the gestures and constant motion sensor, is used to compare the corresponding sensor data report.
 
Sensor:
 
Sensor as an example, this class provides methods can be used to determine the sensor information.
 
SensorEvent:
 
Service use this class to create a sensor event object, which provides a sensor data, type, and the accuracy of the data.
 
SensorEventListener:
 
Sensor event listener, two events can be monitored, sensors precision numerical change and change.

Visit system sensors

IWOP sensor service framework allows you to easily at run time to obtain a list of supported equipment sensors and sensor. To access the sensors need sensors connected services in the first place.You need to instantiate ServiceClient and pass SERVICE_SENSOR as parameters.Such as:


private ServiceClient mClient = new ServiceClient(this,ServiceManagerContext.SREVICE_SENSOR); 
...
mClient.connect();

Then, you can get after service connection sensor and sensor list:


private SensorServiceManager mService;
...
public void onConnected() {
    mService = (SensorServiceManager) mClient.getServiceManagerContext();
    List mService.getSensorList();
}

You can also obtain the specified type sensor:


private SensorServiceManager mService;
private Sensor mSensor;
...
public void onConnected() {
    mService = (SensorServiceManager) mClient.getServiceManagerContext();
    mSensor = mService.getDefaultSensor(Sensor.TYPE_HEART_RATE);
    if (mSensor != null) {
        // Success! There's a heart rate sensor.
    } else {
        // Failure! No heart rate sensor.
    }
}

Monitoring sensor events

To monitor sensor data you need SersorEventListener listener events in two callback methods: onSensorChanged () and onAccuracyChanged ().Sensor service will be back this two methods when:

 
The sensor precision changes:
 
This case will be called onAccuracyChanged () method, now only supports the heart rate sensor processing precision of the change.
 
At present there are two kinds of report of the heart rate sensor precision values, ACCURACY_HEART_RATE_UNAVALIABLE and ACCURACY_HEART_RATE_AVALIABLE respectively. The two described the working state of the heart rate sensor, respectively.
 
When reporting accuracy for ACCURACY_HEART_RATE_UNAVALIABLE, said the heart rate sensor working status is not normal, it is possible that the heart rate is not normal human body skin contact, or discomfort to measure heart rate watch worn timeout.When reporting accuracy for ACCURACY_HEART_RATE_AVALIABLE, said the heart rate sensor working state is normal, watch to wear is good, the heart rate measurement data is reported (usually within 1 ~ 5 s report heart rate data).
 
The sensor data changes:
 
This case will call the onSensorChanged () method, incoming SensorEvent as parameters, the SensorEvent contains a new sensor data and the timestamp event occurs.

The following is a typical usage:


public class SensorActivity implements ConnectionCallbacks,
        SensorEventListener {
    private SensorServiceManager mService;
    private ServiceClient mClient;
    private Sensor mSensor;

    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mClient = new ServiceClient(this,
                ServiceManagerContext.SERVICE_SENSOR, this);
    }

    @Override
    public void onConnected() {
        mService = (SensorServiceManager) mClient.getServiceManagerContext();
        mSensor = mService.getDefaultSensor(Sensor.TYPE_HEART_RATE);
        mService.registerListener(this, mSensor,
                SensorServiceManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onDisconnected(boolean unexpected) {
        mService.unregisterListener(mListener, mSensor);
    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {
        float heartRate = event.values[0];
        // Do something with this sensor value.
    }

    @Override
    protected void onResume() {
        super.onResume();
        mClient.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mClient.disconnect();
    }
}

In the example above USES the SensorServiceManager. SENSOR_DELAY_NORMAL registration as parameters to sensor event listeners, but current sensing delay specified service does not support the event.Example in onResume () and onPause connect/disconnect sensing service, such cancellation in time when you do not need to use the sensor listener closed sensor can save electricity equipment.