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.
1 2 | mSensorManager = (SensorManager) getSystemService( Context.SENSOR_SERVICE ); |
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:
1 2 | List<Sensor> sensorList = mSensorManager.getSensorList( Sensor.TYPE_ALL ); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | TYPE_AMBIENT_TEMPERATURE TYPE_DEVICE_PRIVATE_BASE TYPE_GAME_ROTATION_VECTOR TYPE_GEOMAGNETIC_ROTATION_VECTOR TYPE_GRAVITY TYPE_GYROSCOPE TYPE_GYROSCOPE_UNCALIBRATED TYPE_HEART_BEAT TYPE_HEART_RATE TYPE_LIGHT TYPE_LINEAR_ACCELERATION TYPE_MAGNETIC_FIELD TYPE_MAGNETIC_FIELD_UNCALIBRATED TYPE_MOTION_DETECT |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | class MainActivity : AppCompatActivity(), SensorEventListener { override fun onAccuracyChanged(p0: Sensor?, p1: Int) { //TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onSensorChanged(p0: SensorEvent?) { p0?.let { getAccelerometer(it) } } private lateinit var sensorManager: SensorManager private var isColor = false private var lastUpdate: Long = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initView() } private fun initView() { sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager lastUpdate = System.currentTimeMillis() } private fun getAccelerometer(event: SensorEvent) { val values = event.values // Movement val x = values[0] val y = values[1] val z = values[2] val accelationSquareRoot = (x * x + y * y + z * z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH) val actualTime = System.currentTimeMillis() Toast.makeText( applicationContext, accelationSquareRoot.toString() + " " + SensorManager.GRAVITY_EARTH, Toast.LENGTH_SHORT ).show() if (accelationSquareRoot >= 2) //it will be executed if you shuffle { if (actualTime - lastUpdate < 200) { return } lastUpdate = actualTime//updating lastUpdate for next shuffle if (isColor) { show_change_sensor.setBackgroundColor(Color.GREEN) } else { show_change_sensor.setBackgroundColor(Color.RED) } isColor = !isColor } } override fun onResume() { super.onResume() sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL) } override fun onPause() { super.onPause() sensorManager.unregisterListener(this); } } |
Wishing everyone a successful application. Thank so much!