Control Sphero using Temperature Sensor in Android

This tutorial describes how to control sphero using Temperature sensor. One of the most interesting topic around Android it is how we can connect our smartphones to other devices or smart devices to get information from them or to control them.
In this post I want to introduce a new way to use our Android phone and explain how we can use it to control Sphero ball.
If you don’t know this incredible ball give a look Sphero site.
Below an image of the Sphero taken from the Sphero press kit site.

Getting started: Control Sphero using temperature sensor

In this Android project, I want to describe how we can integrate the temperature sensor inside the smart phone to control the Sphero ball color. In other words, I want to change the color ball according to the temperature measured by the smart phone even if the smart phones is in the stand by mode or the Activity is not in the foreground.
This is an interesting project because it can be used to describe some important concept:

and finally, but not less important, how to connect and use Sphero ball with its SDK. What we want to design an app like shown below:

Design the app

Now we know what we want to obtain, we can mix Android features and components to get it. We need a component that monitors the temperature sensor and another one that connect to the Sphero. As said before, we want to make this component working even if the app is not in the foreground or the smartphone is not active. So we need a Service, because this Android component can fulfill our requirements. The components we will use to control Sphere using temperature sensor in Android are:

An Activity that is the app UI

A Service that monitors the temperature sensor

A Service that connects to the ball and control its color

Looking at the pic below, we can notice that the UI Activity starts two services and listens for events coming from these services, in more details the Activity set up an alarm that is used to start the Temperature Sensor Service so that we won’t drain the battery. The alarm can be configured to start every fixed amount of time. Every time the Temperature Sensor starts it measure the environment temperature using the smartphone sensor and broad cast the value. The UI Activity listens these events and shows the value to the UI, at the same time the Ball Connection Service listens for the same event and as soon as it gets the event, this service calculates the color components (R,G,B) and set the ball color.

Create Temperature Sensor Service: code

Now we have an overview of the main components in our app, we can start coding it. The first element, in the control Sphero using Temperature sensor, we want to code is the Temperature Sensor, service that reads the current temperature. As we know we need a service:

The code seems complex but it is really simple if you look at it carefully. We start broadcasting the event that we are trying to connect to the Sphero (line 3), then, using Sphere API, we register a listener to know when the connection is established and broadcast a new event that the connection is active, at the end of this method we start discovering if new Sphero in around and ready to connect.

The last part of the service is used for listening to the temperature event and set the colour ball:

Create the Activity

The last step is creating the Activity that controls the UI and starts and stops the service. We provide two action bar buttons: one to start the services and another one to stop them. If we touch the start service, we start the AlarmManager to schedule when to run our service:

In this simple code, we create a PendingIntent and get a reference to the AlarmManager, finally we schedule the alarm so that the service can be started after a fixed amount of time. (line 3). In createAlarm() method we setup the intent:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.