Sensor Android TYPE_ACCELEROMETER (part 1)

Tram Ho

One of the things that makes development for mobile devices different from other platforms is a mobile phone or tablet packed full of sensors and hardware that developers can take advantage to do input data. In this tutorial, you will be introduced to Android’s Sensor Framework. You will learn how to identify the sensors available on a device and how to read data from that sensor.

Sensor of the device

There are three main types of sensor to note: motion sensor, environment, and device position. The motion sensor detects changes around the device’s three axes: X, Y and Z, as shown below: Motion sensors include gyroscopes, accelerometers and vector rotation sensors.

Environmental sensors collect information about the environment the phone is in. These include humidity and ambient temperature, light and pressure sensors.

The ultimate basic sensor type, which is device location, does exactly what you expect: recognizes the location of the device. This category includes rotation sensors and magnetic sensors.

Any sensor that is not one of the three basic types is considered a mixed sensor. These sensors are not literally sensors, but instead collect their data from multiple physical sensors on a device and aggregate it. Examples include step count, gravity sensor and vector rotation sensor.

Read sensor data

Discover sensors on devices To access sensors on a device, you’ll need to use the SensorManager class, which you can get as a system service from an Activity.

Once you have a SensorManager, you can get a basic Sensor, or get a List (list) of Sensor objects based on a parameter type. For this example we will access the List of all Sensor objects, as follows:

You will notice that we use TYPE_ALL here. If we only want a smaller set of sensors, we can pass in any of the supported attribute types, such as below.

Once you have the sensors you want to work with, you can start retrieving data from them.

Sensor Event Listener

The following example is an Event sensor TYPE_ACCELEROMETER

Wishing everyone a successful application. Thank so much!

Share the news now

Source : Viblo