Most mobile devices today have built-in sensors such as accelerometers, compasses, temperature, audio and light sensors, GPS etc., that are used in a variety of apps from games to weather and travel apps. The availability of these sensors in mass-marketed mobile devices creates exciting new opportunities for data mining applications.
This blog is going to show you how to display live accelerometer data in a simple Android seismograph app. To that end we’ll use the Telerik Chart control for Android and we’ll test how the control behaves when handling large amount of streaming data. We’ll build the app using Android Studio and (spoiler alert!) this is the result we’ll get at the end:
Let’s dive in.
Once you set up a new application (project) in Android Studio, you need to reference the Chart in your Android app. To keep any unnecessary code and logic out of the post I will focus mainly on the workflow and core mechanics of such applications.
When working with sensors in an Android application, one must first implement the SensorEventListener interface. This interface is used for receiving notifications from the SensorManager when sensor values have changed. Couple of methods have to be overridden - onSensorChanged and onAccuracyChanged. onAccuracyChanged is called when the accuracy of a sensor has changed and onSensorChanged when sensor values have changed. In the case of this seismograph simulator, all the magic will happen in the onSensorChanged override, which is where the acceleration sensor reports its data.
Let’s start by obtaining a reference to the SensorManager and the accelerometer. As the documentation states, in order to get an instance of the default acceleration sensor, we need to do the following:
With the above in mind, any similar accelerometer app would inevitably boil down to the following basic workflow:
Leaving out any unnecessary logic, this is how it looks like in code.
The code above is simplified for illustrative purposes, but is valid for most of the applications of this type. The only thing remaining is to show you how to create the chart.
This is how you can display data from a smartphone sensor (in this case the accelerometer) inside the Chart.
The app source code is available for download on GitHub: https://github.com/telerik/Android-samples/tree/master/Blogs/Seismograph/V1
In the next blog post I’ll show you how to add a “needle”. The needle will be synchronized with the Y coordinate of each incoming accelerometer (“seismic”) data point, which will create the following effect:
Download Telerik Chart for Android from here
Read more about UI for Android here
Hi, I'm Kiril and I'm the Product Manager of Telerik UI for Android, Windows Universal and Windows Phone. Feel free to ping me on +KirilStanoev or @KirilStanoev
Copyright © 2016, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.