Get to Know the Ambient Temperature Through SMS

February 25, 2015 0 comments

We use the temperature sensor of the mobile to create an application where your mobile will respond to an SMS inquiry about the temperature at your place  

– Amaresh Patnaik                                                               

In my last few articles we have been developing a safety application that will respond to queries from a caretaker over SMS. The last one in the series was where the application responds to when it was that you last accessed your mobile phone. We extend the application in this article where your mobile will respond to an SMS enquiry about the temperature around you. This application will depend on the temperature sensor of your device. Make sure you check your handset’s documentation if it has a temperature sensor. The sensor will measure your ambient temperature, that is, the temperature of your surroundings.
The Solution
We will use the project we used in the previous article mentioned in the first paragraph. Open the SMS receiver class and add a class level variable for the hashtag to find out the temperature.
private final String FINDOUTTEMPERATURE = “#FindOutTemperature#”;
In the ‘for’ loop of on receive, add the stub if the message is to find the temperature.

if (strMessage.equals(FINDOUTTEMPERATURE)) {


From the project explorer of Eclipse, copy the light service class and paste it as the temperature service class.

Here is the revised code for the class.

public class TemperatureService extends Service {

private String strTo;

private SensorManager smrTemperature;

private float fltTemperature = Float.NaN;

private final SensorEventListener selTemperature = new SensorEventListener() {

public void onAccuracyChanged(Sensor sensor, int accuracy) { }

public void onSensorChanged(SensorEvent event) {

fltTemperature = event.values[0];





public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

return null;


public void onCreate() {



public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);

Bundle bunSMS = intent.getExtras();

strTo = bunSMS.getString(SMSReceiver.PHONENUMBER);

smrTemperature = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensor snrTemperature = smrTemperature.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);

if (snrTemperature != null) smrTemperature.registerListener(selTemperature, snrTemperature, SensorManager.SENSOR_DELAY_NORMAL);



public void sendMessage() {

SmsManager smsLight = SmsManager.getDefault();

DecimalFormat dcfTemperature = new DecimalFormat(“##.0”);

//Round off to one place after decimal

smsLight.sendTextMessage(strTo, null, “The temperature here is: “ + dcfTemperature.format(fltTemperature) + “ degrees centigrade”, null, null);





Complete the ‘if’ stub we had started off in the SMS receiver class as follows.

if (strMessage.equals(FINDOUTTEMPERATURE)) {

processMessage(context, TemperatureService.class, strFrom);


Register the temperature service in the manifest. The temperature sensor works on the lines of the light sensor we have discussed previously in the safety application, so I have not discussed the working in details. You can similarly make any sensor on your device available to a caretaker through SMS. The caretaker need not have any application installed on his or her device and nor would you require an internet connection.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.