A Quick Take on Reading Sensor Values in Android Studio
Your app might be cool but can it do THIS?!?!
*shakes phone violently and then rotates it counterclockwise to move a ball*
Your answer to that question is probably a hard “No”, but that’s alright. In this article I’m going to give you all of the basics needed to start reading and using sensor data in your Android mobile app. I’ll be using fragments since those are all the rave these days.
What sensors do you have?
In your active fragment:
- Establish a lateinit variable for both your MainActivity and your SensorManager
- Assign values to those variables
- Use the getSensorList method of your SensorManager to get a list of…sensors
- Loop through that list and output the result to your debug log
Note that if you are coding this in a Fragment and not an Activity, you need to use the context of the MainActivity. If you try to follow the Android documentation instructions word for word, you’ll get an error when you type getSystemService.
Code to copy: https://gist.github.com/jtmuller5/82a576d1f7f144a1b2ae522ab6ada685