Android’s Gyroscope

Learning to use the sensor’s in an Android machine is something I should have done a while back. One of the things I’m liking about the Android system is that it seems to provide access to what I assume is pretty complex technology with simple sensor classes (even if Android Studio still bugs me to a great extent).

The application I’ve made uses the change in the gyroscope’s angle in order to draw onto the canvas. Basically, a tilt of the tablet changes the direction in which the controller on screen moves and consequently the direction in which the line is painted.

Building this involved a couple of interesting little tricks: inheriting from the android view class, overriding the onDraw method, and learning to register and unregister android services. Actually figuring out how to stick a compiled application directly onto a device was a bit of a pain; that tap the model number 7 times to get the developer mode trick is very strange.

The main goal of this was to learn how to use the built in android sensors, so the drawing implementation is not exactly efficient. In order to correctly draw the line, the view I’ve implemented stores a list of previous positions and draws a circle the same size of the character at that point; at the moment there’s no limit on the size of the list so it will inevitably cause a crash if there are too many changes in the screen’s orientation.

One of the things I didn’t consider when making this program was that I’ve set the speed of the character based upon the change in rotations (with the addition of some threshold values to make sure it can continue one direction and doesn’t just travel forward and back), but because of differences in the screen size I can turn on one axis a lot quicker. That’s something to consider for the future, should I ever put this to use.

The gyroscope sensor code I’ve used in this code can be found in the below videos (neither of which are my own content). The first targets the gyroscope sensor explicitly, the second is targeting an accelerometer, but I think it gives a better insight into the event values array that the sensor events provide. These are the X, Y, and Z rotations in the case of the gyroscope, and that took a while to wrap my head around.

Video 1 – Last accessed 21/05/2017

Video 2 – Last accessed 20/05/2017

As an afterthought, it wouldn’t be a bad idea to monitor the change in angle to calculate acceleration. This is more physics work than anything sensor-based, but it’d be a cool way to improve the app.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s